File Handling in Python

File handling in Python with examples.

A file is a resource for recording or storing data on a computer device.

There are 4 types of I/O operation for files

  1. Open File
  2. Read File
  3. Write File
  4. Close File

Open file in Python

In Python, we use the open() method to open files. The open method takes at least one argument: filename for file operation.

Let’s consider the below file.

file handling-in-python-file-content

Below code will open the file.

file handling-in-python-open-file-method

open() function opens the file and makes it available for read and write operation. The filename is a mandatory argument. By default, it opens the file in read mode if no mode is specified explicitly. We can pass the mode of the file as per the requirement to open the function which is an optional argument. 

There are different modes of file for I/O Operation.

Modes of fileDescription
rOpen a file for reading.
wOpen a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
xOpen a file for exclusive creation. If the file already exists, the operation fails.
aOpen a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.
tOpen in text mode. 
bOpen in binary mode.
+Open a file for updating (reading and writing)

Below are a few examples of opening file in different modes

file handling-in-python-open-file-examples

Read files in Python

For reading files we use the read() method. Filename and read mode is passed as an argument to the open method.

file handling-in-python-read-file


file handling-in-python-output-file

Here file function is used to read the contents of the file

There are different functions to read files in Python

read() – By default function returns the whole text, but you can also specify how many characters you want to return.

readline() – function is used to return one line in file

readlines() – method returns a list of all lines in the file.

Write file in Python

For writing files we use the write() method. Filename and write mode is passed as an argument to the open method. If we try to open a file that is not found or doesn’t exist, a new file is created.

If a file already exists or is already created, its content is erased, and new content is added to the file. In order to write into a file in Python, we need to open it in write mode by passing “w” inside open() as a second argument.

file handling-in-python-write-file


file handling-in-python-output-read-file

Here file pointer.write() function is used to write contents in the file

There are different functions to write files in Python

write() – the text or byte object that will be inserted in the file. Writes the specified string to the file

writelines() –  this function writes a list of strings to the file

To write to an already existing file, you must pass the below arguments to the open() function:

“a” – Append – will append to the end of the file

“w” – Write – will overwrite any existing content

Close file in Python

Once all operations are done with the file. We must close the file object.

Closing the file after I/O operations frees up the resource so that the same resources can be used for some other tasks by the system.

close() – This method is used to close the file.

file handling-in-python-close-file

In the above code we have used the close() function to close the file.

Python makes sure that any unwritten or unsaved data is flushed off to the file before it is closed.
Always advised to close the file once our work is done. Also, if the file object is re-assigned to some other file, the previous file is automatically closed.

Please install Pycharm and start coding.

Recommended Articles

Leave a comment