Python File handling - My 4th week of learning python by Nishant Kalanta

This is my 4th week of learning Artificial Intelligence/Data Science. I have started Python which is my first step towards learning Machine learning. Like last time on my previous blog, I told you about modules and packages used in Python. Today I a going to tell you all about the next topic that we studied while studying python i.e. “File handling”. 

The concept of file handling has stretched over various other languages, but the implementation is either complicated or lengthy, but alike other concepts of Python, this concept here is also easy and short. Each line of code includes a sequence of characters and they form a text file and Python treats files differently as text or binary. We use file handling for the storage of the output earlier the program we are performing was stored but their output was not storing but file handling provides us feature to store data and keep a record just like the database. Then the question arises why will we use file handling as a database to provide us more and easy features to store data, Answer is a simple database that can hold a huge amount of data but it is difficult to manage that amount of data on file handling. Another answer to this question can be database uses a large space for storing a small amount of data that means if we want to store a small amount of data database will set a large space to store data. That's why we use file handling over the database for certain conditions. There are 2 types of function used in file handling in Python i.e

1) Open - This function is used to open a file in the Python environment to perform different mode's on the file.

2) Close - This function is used at the end of every program to make the program know that function has completed and to terminate the process. If we have not closed the program there will be no changes in the program that we have used using different modes.

The basic mode's used in File handling are as follows:

1) Read()

In this function, we use file just for reading purposes we can't make any changes in file during the execution of the program. The cursor position would at the start of the program

2) Write

In this function, we use the file for writing purposes only. If we open the file using Python and that file doesn't exist it will create a new file and then we can perform the function on that file and if we have previous content in this and we open file with Python with write mode it will overwrite the new data over previous data means when we open a file in write mode it will delete all data in that file and start that file form being. This mode can be used when we have create new data every time. The cursor position would at the start of the program

3) Append

This function is used just like write mode but a basic difference is that it will not overwrite the data it will continue the write function from that line from where we have left means cursor position will be at the end of the program and from there will start our task on text file. The cursor position would at the end of the program

4) Read+

In this mode, we can read and write at the same time without affecting the previous data while opening the text file.

5) Write +

In this mode, we can write and read at the same time but when we will open the program the previous data will overwrite like write mode but we can read text file at the same time and the cursor position would at the start of the program.

6) Append + 

This like read+ mode but the cursor position would at the end of the program.

Basic terminology used in file handling are:

1) Cursor 

The current position of the pointer in the file is known as cursor.

2) Seek

This command is used to control the position of the cursor i.e if we set the value of seek 0 means the cursor is at the beginning, 1 for the current position and 2 for the end of the program.

0 views0 comments

Contact Us:

A-116, The Corenthum A-65,

Sector 62

Noida 201301

UP, India

Phone: +91 8882050481


© 2020 TheIkigaiLab  Terms of Use Privacy and Security Statement