Link Search Menu Expand Document

File Handling in Python

The named locations on the hard disks for storing data or information in the non-volatile memory are known as files.  Since the Random Access Memory (RAM) is volatile, the user loses his data the moment he switches off the plug. Therefore, the users utilize the file storage to store the information permanently.

File handling is the storing, changing, reading data from the file using a program. The concept of file handling is common in various programming languages. However, in some languages, the implementation is not easy and is a lengthy process. Like many other functions, Python, being a user-friendly language, provides an easy implementation for file handling.

File Handling in Python

Python provides various functions to its users to handle the files, i.e. to create, read, and write files and to perform various functions on the files.  When handling files, the user must open that file first to perform any operation on it, and close the files after he has completed them to free the tied resources of that specific file.

Opening file in Python

Python language provides an in-built function, open (), to open a file in the program. The user specifies the file name and the file extension in the function’s parameter to open that particular file. Moreover, the user can open more than one file in a single program by calling the function multiple times. However, it is essential to mention the file path if the specified file is not present in the program’s current directory.

File1 = open(“filename.txt”)        //opening the file from current directory
File2 = open(“C:/user/filename.txt”)        //specifying the path

File Opening modes

While opening the file in Python, the user needs to mention the mode in which he wants to open the file. By default, the program opens the file in “read in the text’ mode, which reads strings from the file. Python can also handle the files in bytes mode in which the user deals with the non-text files such as executable (exe) or image files. Therefore, the open() function takes one more argument for the mode, and open the specified file in the user-defined mode.

Following are the modes used in the open() function:

  1. “r”, for opening file in reading mode
  2. “w”, for opening file in write mode
  3. “b”, for opening file in binary mode
  4. “x”, for opening file in exclusive creation mode
  5. “a” for opening file in append mode
  6. “t”, for opening file in text mode
  7. “+”, for opening file in update mode, i.e. read and write

Moreover, whenever the user opens a file to write, the program always truncates it if he has not opened it n the append mode. Additionally, the write and append mode creates the file if the file does not exist in the specified directory.

Writing to the file

In order the write in the file, the user needs to open the file in write, append, or exclusive mode. Then, the user can write to the file using write () which takes a string as its input.

File = open(“filename.txt”, w)
File.write(“Hello world”)

Reading from file

The user must open the file in the read mode to read from it. Then, he can use the read () function to read from it. This function takes the size as its argument to read that many characters from the file. In the scenarios, where the user has not specified the size, the function reads the whole file. Furthermore, the user can also specify the exact position from where he wants to read or write by using the seek () function.

File = open(“filename.txt”, r)
print(File.read(5))
print(File.read())
print(File.seek(3))

Closing file

It is a good programming practice to free the resources after use. Like other tied resources, the users need to close the opened files as well. Like many other languages, Python also has a garbage collector that de-references the unused objects. However, users can never be sure of when it de-references them. Therefore, in Python, there is a function called close () to close the opened files.

File = open(“filename.txt”)
File.close()

Note: Once the user has closed the file, he cannot access it without opening it again.

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy