In this tutorial, you will master all about python files and file operations with examples. You will cover what a file is, how to open and close a file, and some basic ways to read and write files in python.
A file is a named location on the system storage which stores related data for future purposes. The data can be anything like a simple text file, video or audio file, or any complex executable programs. We use the file system to enable persistent storage in non-volatile memory like a hard disk. Files consist of three important parts as listed below.
Next, when it comes to accessing a file we must know where we have stored the file. For that, we use the File path which gives the route or location of the file. The file path contains 3 parts as listed below.
There are two types of files in python. They are:
When you want to work with files, you need to perform the 3 basic operations which are listed below in the order of processing.
The above flow chart gives you the workflow of file handling in python. Initially, we need to create a file. This can be done manually by the user wherever he needs and save it with a valid filename and extension. Also, python supports file creation which you will encounter in future sessions.
The first and foremost operation with a file is opening the file. A file can be opened by invoking the python built-in function open()
. The open function returns the file object which can be used later to read or write the file.
File object = open(file_name [,access_mode] [,buffersize])
Where,
open("testfile.txt")
open("testfile.txt",'r')
Here is the table showing different opening modes of files.
Modes | Meaning | Description |
---|---|---|
r | Read | Default mode.Open file for reading. Error if the file does not exist |
w | Write | Opens file in write-only mode to overwrite. Creates a file if it does not exist |
a | Append | Appends the file at the end of the file without trimming the existing. Creates a file if it does not exist. |
x | Create | Creates the defined file. Error if the file already exists. |
b | Binary | Opens file in binary mode |
t | Text | Opens file in text mode. Default mode. |
r+ | Read and Write | Opens file for both reading and writing |
rb | Binary read | Opens file in read and binary mode |
rb+ | Binary read and write | Opens file in binary mode for reading and writing |
w+ | Read and write | Opens file in writing and reading mode |
wb | Binary write | Opens file in writing and binary mode |
wb+ | Binary read and write | Opens file in binary mode for writing and reading |
a+ | Append read and write | Open file in appending mode for reading and writing. |
ab | Binary append | Opens in append mode and binary mode |
ab+ | Binary append read and write | Opens file in read write binary mode for appending |
You can use several attributes to get the details of the file once after a file object gets created. Some of the attributes are listed below.
Note: it is always a good practice to specify the encoding while dealing with a file in text mode.
Closing a file is not at all a big task however it is the responsibility of the user to properly close the file once the work gets completed. Python supports two ways to close a file which are described below with examples.
We can close an opened file using the close() method which is available in python. This free up the resources that were secured with the file.
f_obj = open(“testfile.txt”)
,.......................
,.......................
f_obj.close()
Even though this is an easy way,it has one drawback i.e., using close method alone does not address any errors or exceptions. When an exception is encountered it immediately exits the code without properly closing the file. So what should we do next?
try...finally, blockTo resolve the above problem we can use try..finally, block which addresses the exceptions whenever it raises and thereby assures the proper closure of the file.
try:
f_obj = open(“testfile.txt”)
,.......................
,.......................
finally:
f_obj.close()
In python use of “with the statement “ is considered as the best way to close a file. The with statement involuntarily takes the control to close the file when it exits the “with block”. So we don't need to explicitly specify the close () method in the code. In other words, using “with the statement” boosts the easiness to handle exceptions and allows for flawless codes.
with open(“testfile.txt”) as f_obj:
……………………………………
…………………………………...
finally:
f_obj.close()
Once we open a file we can perform the desired operation on it. The two operations that can be done on a file are read and write operation. First, we can look at how to read a file. To read a file, the first and foremost thing we have to do is to open a file in read mode (r). Assume we have the following contents in our file named testfile.txt in the same location in python.
testfile.txt
Hello World Welcome to Programming Tutorials We Love Python See You..Bye
Below given are the three methods that can be called on a file object to read the file.
Using this method we can read a file to the specified size bytes.
f_obj=open("testfile.txt",'r')
print(f_obj.read(6)) #reads the first 6 characters(Hello ) in the file
print(f_obj.read(4)) #reads the next 4 characters(Worl) in the file
f_obj.close()
In the above example, size is specified as 6
so it reads the first 6
characters only and returns the same. The next time the size is mentioned as 4
, so it reads the next 4
characters in the file after Hello (first 6
characters). This indicates that the read() method returns a newline after each function call.
The read()
method reads the entire file when :
The following example gives clarity for this concept.
f_obj=open("testfile.txt",'r')
print(f_obj.read(-1))
Output:
Hello World Welcome to Programming Tutorials We Love Python See You..Bye
This method is used to read each line in the file and this will return a line by line output. Let see in the below example how the readline method works.
f_obj=open("testfile.txt",'r')
print(f_obj.readline()) #reads first line in the file
print(f_obj.readline()) #reads second line in the file
print(f_obj.readline()) #reads third line in the file
f_obj.close()
Output:
Hello World Welcome to Programming Tutorials We Love Python
Similarly when arguments passed are None or -1, the function returns each individual line. However, when we specify the size, something different will occur. Instead of returning lines, it returns the characters to the specified size as in the read method.
f_obj=open("testfile.txt",'r')
print(f_obj.readline(6))
print(f_obj.readline(4))
print(f_obj.readline(-1))
f_obj.close()
Output:
Hello Worl d
readlines()
method returns all the lines in the form of a list. For each line, end of the line is denoted as \n. The above example can be changed to :
f_obj=open("testfile.txt",'r')
print(f_obj.readline(6))
print(f_obj.readline(4))
print(f_obj.readlines()) #returns remaining lines as a list
f_obj.close()
Output:
Hello Worl ['d\n', 'Welcome to Programming Tutorials\n', 'We Love Python\n', 'See You..Bye']
Note: When EOF is encountered all the reading modes will return empty values.
Another way to read a file in line by line manner is by iterating through each line. This can be accomplished using either for loop or while loop with readlines method. The following example will illustrate two different ways to print lines in a file.
with open("testfile.txt",'r') as f_obj:
for line in f_obj:
print(f_obj.readlines())
Output:
(['Hello World\n', 'Welcome to Programming Tutorials\n', 'We Love Python\n', 'See You..Bye']
with open("testfile.txt",'r') as f_obj:
for line in f_obj.readlines():
print(line ,end =' ')
Output:
Hello World Welcome to Programming Tutorials We Love Python See You..Bye
However, the above example can be changed to another form to simplify it. Here instead of looping the methods, we iterate the file object.
with open("testfile.txt",'r') as f_obj:
for line in f_obj:
print(line ,end =' ')
Output:
Hello World Welcome to Programming Tutorials We Love Python See You..Bye
To write into a file we need to make use of two modes -write mode and append mode. We have already discussed that a file opened in write mode gets overwritten by erasing all the existing lines. Whereas when a file is opened in append mode it appends the new text into the file without truncating the previous text. The builtin method to write to a file is write() method.
Following examples shows the difference between write mode and append mode :
with open("testfile.txt",'w') as f_obj:
f_obj.write('OVER WRITES FILES')
When we execute the above code, what happens is the textfile.txt gets overwritten as shown in the image below.
Now we are opening the same file in append mode and adding some text to the file as given in the example below.
with open("testfile.txt",'a') as f_obj:
f_obj.write('Here iam opened the file in append mode and appends this statment')
The output shows a significant difference. Here the file is not overwritten instead of appends the text at the end of the existing text.
The other possible way to find the difference in the file after writing or appending is to use again and open the file in read mode and print the file.
To create a new file in python, open the file with any of the below modes.
“x
”= create: creates a file, returns an error when a file exists with the same file_ name.
“a
”= append: creates a file when the defined file does not exist.
“w
”= write: creates a file when the defined file does not exist
open('New_file_x.txt','x')
open('New_file_a.txt','a')
open('New_file_w.txt','w')
Output:
From the above example, it is clear that Python has created 3 text files using the aforementioned modes in your system. Now let's check what will happen when we try to create an existing file. Obviously python will raise an error i.e, FileExistsError as shown below.
open('New_file_a.txt','x')
Output:
open('New_file_a.txt','x') FileExistsError: [Errno 17] File exists: 'New_file_a.txt'
To circumvent this error we can either rename the file to a new name or delete the existing file if it is not an important one
It is possible to rename or delete an existing file. For renaming, we use the built-in method os.rename()
, and for removing a file from the system method os.remove
is used. Both these methods reside in the OS module. So it is necessary to import the OS module before performing any of these tasks. The OS module which comes under Python standard utility module helps us to interact with the Operating system and thereby provides the way to use OS-dependent functionality.
The rename method changes the name of an existing file with a new name. The basic syntax of the rename method is :
os.rename(‘Current_File_name’,’New_File_name’)
import os
os.rename('New_file_x.txt','New.txt')
The result will be reflected in your system with a change in name of the file.
We can delete a file in two possible ways. One way is to manually search the file in the system and delete the file. The otherways is by standing in the interactive shell and delete files using python methods.
To delete a file we use the built-in method os.remove()
which Syntax is
os.remove(‘File_name’)
Before attempting to delete a file we need to check whether it exists or not to avoid any unwanted errors. We can use the method os.path.exists()
to check the existence of a file as shown in the below example:
import os
if os.path.exists('New_file_a.txt'):
os.remove('New_file_a.txt')
print('Removed file')
else:
print('File doesnot exist')
Throughout this tutorial, we have encountered some of the methods used to manipulate files. The below table gives the whole list of file methods used in python.
Method | Description |
---|---|
close() | Closes an open file. |
fileno() | Returns the number of file descriptors. |
flush() | Flushes the write buffer of the file stream. |
isatty() | Returns True when the file stream is interactive else false |
read() | Reads the entire file. |
readable() | Checks whether the file is readable or not |
readline() | Reads individual lines in a file. |
readlines() | Reads the entire file and returns as list |
seek() | Changes the position of the file |
seekable() | Checks whether the position is changeable or |
tell() | Returns the current position of the file |
truncate() | Resizes the file to a specified size.If not specified, resizes to the current position. |
writable() | Returns True when the file is writable |
write() | Writes the defined string to the file. |
writelines() | Writes a list of strings or lines to the file. |