How to perform File I/O operations in Python

Hello guys, welcome back to programminginpython.com! Here in this post, I will show you how to perform different File I/O operations like opening a file, reading content from the file, writing data to a file and all the stuff. You can do most of the operations I just mentioned by using a file object.

Program on Github

Check this video on youtube here.

Opening a file

If you want to perform any operations like reading, appending or writing, first, you must open a file. You can open a file by using a Python’s built in function open(). This open() function take 3 arguments, they are the file nameaccess mode and buffering.

 

The 3 parameters,

file_name – This parameter contains the name of the file which you want to access or perform some operations on it.

access_mode – This access_mode determines in which mode the file should open, i.e reading(‘r‘), writing(‘w‘), appending(‘a‘), reading and writing(‘r+‘), reading in binary format(‘rb‘) and many other modes. Below is a list of all the access modes.

File I/O operations in python - programminginpython.com
File I/O operations in Python – programminginpython.com(from Python docs)

buffering – This buffering is an optional parameter if you want buffering option you set its value to 1, else 0 for no buffering, if you set it greater than 1, it will start buffering from that buffer size.

File Attributes

Once a file is opened, you can get the values of different attributes like the name of the file, mode of the file, whether the file is opened or closed.

When you execute the above snippet, it produces the following output.

File I/O operations in python - programminginpython.com
File I/O operations in Python – programminginpython.com

Program on Github

Writing to a file

For writing content to a file, you first need to open() it in writing mode, any of w, w+. wb, wb+ these modes can be used to write content to a file.

You can write either in binary format or normal text format. If you write in a binary format you need to convert the content to bytes.

Without binary format,

Both the above snippets create a new file if the file does not exist and if the file exists, it overwrites the content.

Program on Github

Reading from a file

For reading the content in a file, you need to open() the file, and then use the Python’s read() function to read the data.

You can open the file in any mode which supports reading a file.

Appending content to a file

When You need to add some extra content to the existing data, you should open the file in append mode, and do the rest as what you will do as in write mode. It is similar to write mode which I have shown in the above paragraphs, the main difference is the write mode overwrites the content but here the append mode adds to the existing content.

So now if I run the above snippet, it adds an extra line to the original content.

File I/O operations in python - programminginpython.com
File I/O operations in Python – programminginpython.com

Program on Github

Delete a file

If you need to delete any file, then, in that case, you can use the Python’s built in function remove() to delete a file. However, you need to import the os module to do this and pass a file name you want to delete as an argument to remove() function.

That is how you can handle different file operations in Python. Below is the complete code snippet related to the file handling I/O operations covered above.

Program on Github

Also please feel free to check other posts on Python Lists, Python Tuples or Python GUI programming.

Leave a Reply