Skip to content

File Handling in Python

Comprehensive Guide to File Handling in Python

File handling is a crucial aspect of programming, allowing you to read from and write to files. Python provides a set of functions and methods that make file handling straightforward. In this comprehensive guide, we will explore various aspects of file handling in Python, covering reading, writing, and manipulating files.

Opening a File in Python

We can open a file in Python using the open()open() function. This function accepts two arguments, the name of the file and the mode in which we want to open the file. The mode can be read mode, write mode, append mode, etc. The default mode is read mode.

Syntax:

open.py
file_object = open(file_name, [access_mode], [buffering])
open.py
file_object = open(file_name, [access_mode], [buffering])

Here,

  • file_namefile_name is the name of the file that we want to access.
  • access_modeaccess_mode is the mode in which we want to open the file, i.e., read, write, append, etc.
  • bufferingbuffering is an optional integer used to set the buffering policy.
  • file_objectfile_object is the object that we can use to access the file.

File Opening Modes

There are different modes in which we can open a file. These modes are:

S.No.ModeDescription
1rrOpens a file in read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.
2rbrbOpens a file in read-only mode in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
3r+r+Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
4rb+rb+Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
5wwOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
6wbwbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
7w+w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
8wb+wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
9aaOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
10ababOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
11a+a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
12ab+ab+Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
13xxOpens a file for exclusive creation. If the file already exists, the operation fails.
14xbxbOpens a file for exclusive creation in binary format. If the file already exists, the operation fails.
15x+x+Opens a file for exclusive creation. If the file already exists, the operation fails.
16xb+xb+Opens a file for exclusive creation in binary format. If the file already exists, the operation fails.
17ttOpens in text mode. (default)
18bbOpens in binary mode.
19++Opens a file for updating (reading and writing)
20UUUniversal newline mode.

Closing a File in Python

When we are done with operations to be performed on the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file and is done using the close()close() method. Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.

Syntax:

close.py
file_object.close()
close.py
file_object.close()

Reading a File in Python

There are various methods available for this purpose. We can use the read(size)read(size) method to read in the size number of data. If the size parameter is not specified, it reads and returns up to the end of the file.

Syntax:

read.py
file_object.read([size])
read.py
file_object.read([size])

Here,

  • file_objectfile_object is the file object that we want to read.
  • sizesize is an optional numeric argument. When it is provided, it reads that many characters from the file. If the size parameter is not specified, it reads and returns up to the end of the file.
  • The read()read() method returns the specified number of bytes from the file. If we omit the size argument, it returns and displays all the data from the file.

Example:

read.py
file_object = open("test.txt", "r")
data = file_object.read()
print(data)
file_object.close()
read.py
file_object = open("test.txt", "r")
data = file_object.read()
print(data)
file_object.close()

Output:

command
C:\Users\username>python read.py
Hello, World!
This is a test file.
command
C:\Users\username>python read.py
Hello, World!
This is a test file.

Conclusion

Understanding file handling in Python is essential for a wide range of applications. Whether you’re reading data from a file, writing to it, or manipulating its content, Python’s file handling capabilities offer flexibility and ease of use. By following the principles outlined in this guide, you can efficiently work with files in Python, enabling you to build robust and scalable applications. Happy coding! For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did