A file is a container in computer storage devices used for storing data.
When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed so that the resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order:
- Open a file
- Read or write (perform operation)
- Close the file
Opening Files in Python
In Python, we use the
open() method to open files.
To demonstrate how we open files in Python, let's suppose we have a file named
test.txt with the following content.
Now, let's try to open data from this file using the
# open file in current directory file1 = open("test.txt")
Here, we have created a file object named file1. This object can be used to work with files and directories.
By default, the files are open in read mode (cannot be modified). The code above is equivalent to
file1 = open("test.txt", "r")
Here, we have explicitly specified the mode by passing the
"r" argument which means file is opened for reading.
Different Modes to Open a File in Python
||Open a file for reading. (default)|
||Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.|
||Open a file for exclusive creation. If the file already exists, the operation fails.|
||Open a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.|
||Open in text mode. (default)|
||Open in binary mode.|
||Open a file for updating (reading and writing)|
Here's few simple examples of how to open a file in different modes,
file1 = open("test.txt") # equivalent to 'r' or 'rt' file1 = open("test.txt",'w') # write in text mode file1 = open("img.bmp",'r+b') # read and write in binary mode
Reading Files in Python
After we open a file, we use the
read() method to read its contents. For example,
# open a file file1 = open("test.txt", "r") # read the file read_content = file1.read() print(read_content)
This is a test file. Hello from the test file.
In the above example, we have read the
test.txt file that is available in our current directory. Notice the code,
read_content = file1.read
file1.read() reads the
test.txt file and is stored in the read_content variable.
Closing Files in Python
When we are done with performing operations on the file, we need to properly close the file.
Closing a file will free up the resources that were tied with the file. It is done using the
close() method in Python. For example,
# open a file file1 = open("test.txt", "r") # read the file read_content = file1.read() print(read_content) # close the file file1.close()
This is a test file. Hello from the test file.
Here, we have used the
close() method to close the file.
After we perform file operation, we should always close the file; it's a good programming practice.
Exception Handling in Files
If an exception occurs when we are performing some operation with the file, the code exits without closing the file. A safer way is to use a try...finally block.
Let's see an example,
try: file1 = open("test.txt", "r") read_content = file1.read() print(read_content) finally: # close the file file1.close()
Here, we have closed the file in the
finally block as
finally always executes, and the file will be closed even if an exception occurs.
Use of with...open Syntax
In Python, we can use the
with...open syntax to automatically close the file. For example,
with open("test.txt", "r") as file1: read_content = file1.read() print(read_content)
Note: Since we don't have to worry about closing the file, make a habit of using the
Writing to Files in Python
There are two things we need to remember while writing to a file.
- If we try to open a file that doesn't exist, a new file is created.
- If a file already exists, 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
open() as a second argument.
Suppose, we don't have a file named test2.txt. Let's see what happens if we write contents to the
with open(test2.txt', 'w') as file2: # write contents to the test2.txt file file2.write('Programming is Fun.') fil2.write('Programiz for beginners')
Here, a new
test2.txt file is created and this file will have contents specified inside the
Python File Methods
There are various methods available with the file object. Some of them have been used in the above examples.
Here is the complete list of methods in text mode with a brief description:
|close()||Closes an opened file. It has no effect if the file is already closed.|
|detach()||Separates the underlying binary buffer from the
|fileno()||Returns an integer number (file descriptor) of the file.|
|flush()||Flushes the write buffer of the file stream.|
|read(n)||Reads at most n characters from the file. Reads till end of file if it is negative or
|readline(n=-1)||Reads and returns one line from the file. Reads in at most n bytes if specified.|
|readlines(n=-1)||Reads and returns a list of lines from the file. Reads in at most n bytes/characters if specified.|
||Changes the file position to offset bytes, in reference to from (start, current, end).|
|tell()||Returns an integer that represents the current position of the file's object.|
||Resizes the file stream to size bytes. If size is not specified, resizes to current location.|
|write(s)||Writes the string s to the file and returns the number of characters written.|
|writelines(lines)||Writes a list of lines to the file.|