File Handling in Python

A file is a collection of data stored on a secondary storage device like a hard disk.

open() function:

This function creates a file object, which will be used to invoke methods associated with it. It has the following syntax:

fileObj=open(file_name [, access_mode])

Here, file_name is a string value that specifies the name of the file that you want to access.

access_mode indicates the mode in which the file has to be opened, read, write, append, etc. access_mode is an optional parameter and the default file access mode is read(r).

The open() function returns a file object. This file object will be used to read, write, or perform any other operation on the file. It works like a file handle.

close() function:

The close() method as the name suggests is used to close the file object. Once a file object is closed, you can’t further read or write into the file associated with the file object. While closing the file object close() flushes any unwritten information. However, python automatically closes a file when the reference object of a file is reassigned to another file. It has the following syntax:


The close() method frees up any system resources such as file descriptors, file locks, etc. that are associated with the file. Moreover, there is an upper limit to the number of files a program can open. If that limit is exceeded then the program may even crash or work unexpectedly. Thus, you can waste lots of memory if you keep many files open unnecessarily also remember that open files always stand a chance of corruption and data loss.

write() and writelines() methods:

The write() method is used to write a string to an already opened file. Of course, this string may include numbers, special characters, or other symbols. While writing data to a file, you must remember that the write() method doesn’t add a newline character (‘\n’) to the end of the string. It has the following syntax:


writelines() method is used to write a list of strings. It has the following syntax:


read() and readlines() methods:

The read() method is used to read a string from an already opened file. As said before, the string can include alphabets, numbers, characters, or other symbols. It has the following syntax:[count])

The count is an optional parameter that if passed to the read() method specifies the number of bytes to be read from the opened file. The read() method starts reading from the beginning of the file and if the count is missing or has a negative value, then it reads the entire contents of the file.

The readlines() method is used to read a single line from the file. The method returns an empty string when the end of the file has been reached.

Note: A blank line is represented by \n and the readlines() method returns a string containing only a single newline character when a blank is encountered in the file. The readlines() method is used to read all the lines in the file.

append() method:

Once you have stored some data in a file, you can always open that file again to write more data or append data to it. To append a file, you must open it using ‘a’ or ‘ab’ mode depending on whether it is a text file or a binary file.

Note: If you open a file in ‘w’ or ‘wb’ mode and then start writing data into it, then its existing contents would be overwritten. So, always open the file in ‘a’ or ‘ab’ mode to add more data to the existing data stored in the file.