Python is one of the most popular programming languages, and unlike some other languages, it does not require you to import a library to work with files.
Files are handled natively in Python; however, the methods of working with files are different from other languages.
In this Python tutorial, you'll learn about opening, reading, writing, and closing files. We will also cover using the "with" statement in this post.
How to Open File in Python?
Python comes with functions that enable creating, opening, closing, reading, and writing files built-in. Opening a file in Python is as simple as using the open() function that is available in every Python version. The function returns a "file object."
File objects comprise methods and attributes which can be used to gather information about the file you open. Furthermore, these details also come in handy when you want to manipulate the file you open.
For instance, "mode" is one of the few attributes that every file object has associated with it. It describes the mode in which the file opened.
Similarly, the "name" attribute of the file object reveals the name of the file opened.
You must remember that while fundamentally related to each other, files and file objects are separate things.
What Is a File?
When you use an operating system like Windows, "files" mean images, documents, videos, audio clips, executables, and the like. At the technical level, files are simply named locations on a disk drive with related data stored in them.
Disk drives are used to store data permanently since RAM is volatile and loses data when the computer is shut down.
In the OS, you can manipulate these files and organize them in folders that form sophisticated directories. Essentially, a file in Windows is any item that you can create, manipulate, and delete.
However, Python deals with files differently than Windows, or any other OS does.
Types of Files in Python
Python classifies files into one of two categories: text or binary. Understanding the contrast between them is critical before you learn how to work with Python files.
Text files are files that have sequences of data, with the data being alphanumeric characters. In these files, every line of information is terminated with special characters known as the EOL or End Of Line characters.
There are many different EOL characters that a text file may use. However, the new line character ("\n") is Python's default EOL character, and hence, the most commonly used one.
The backslash in the new line character indicates to the Python interpreter that the current line has ended. The "n" after the backslash indicates to the interpreter that the data following must be treated as a new line.
Binary files are starkly different from text files. As the name indicates, binary files contain binary data – 0s and 1s – and can only be processed by an application that understands binary.
These files do not use EOL characters or any terminators whatsoever. The data is stored after it is converted into binary.
Note: Python strings are different from files, but learning how to work with strings can help better understand how Python files work. To learn more about working with strings in Python, check out our comprehensive guide on strings.
Opening a Text File
Before you can write to or read from a file, you must open the file first. To do this, you can use the open() function that comes built into Python.
The function takes two arguments or parameters: one that accepts the file's name and another that saves the access mode. It returns a file object and has the following syntax:
file_object = open("File_Name", "Access_Mode")
You must note that the file you're opening must be in the same folder/directory as the Python script. If the file isn't in the same directory, you must mention the file's full path when writing the file name parameter.
For instance, to open an "example.txt" file that's in the same directory as the script, you'd write the following line of code:
f = open("example.txt")
On the other hand, to open an "example.txt" file on the Desktop, you'd write the following code:
f = open("C:\Users\Krish\Desktop\example.txt")
Python has several access modes, and these modes govern the operations that you can perform on an open file. In other words, every access mode refers to how the file can be used when it's open differently.
Access modes in Python also specify the position of the file handle. You can think of the file handle as a cursor indicating from where the data must be read or written.
That said, using the access mode parameter is optional, as seen in the previous examples.
There are six access modes in Python:
- Read only ('r'): It is the default access mode. It opens the text file for reading and positions the file handle at the start of the file. If Python finds that the file does not exist, it throws an I/O error and does not create a file with the specified name.
- Write only ('w'): It is used for writing files. If the file isn't in the folder, a file with the specified name is created. If the file exists, the data is truncated and then over-written. The file handle is positioned at the start of the file.
- Read and write ('r+'): You can both read and write on files opened with this access mode. It positions the handle at the start of the file. If the file isn't in the directory, it raises an I/O error.
- Write and read ('w+'): Like the previous mode, you can both read and write on files opened with this access mode. It positions the handle at the start of the file. If the file isn't in the folder, it creates a new file. Furthermore, if the specified file exists, the data in it is truncated and over-written.
- Append only ('a'): This mode opens the file up for writing. If the particular file isn't in the directory, a new one with the same name is created. However, unlike other modes, the handle is located at the end of the file. Thus, the data entered is appended after all of the existing data.
- Append and read ('a+'): It is used to read and write files in Python. It works similarly to the previous mode. If the file isn't available in the directory, one with the same name is created. Alternatively, if the file is available, the entered data is appended after the existing data.
The "+" in the access mode parameters essentially indicates that the file is being opened for updating.
There are two other parameters you can use to specify the mode you want to open the file in.
Python reads files in text mode by default, which is specified with the parameter "t." The open() function returns strings when reading the file. In contrast, using the binary mode ("b") when using the open() function returns bytes. Binary mode is typically used when handling non-text files such as images and executables.
Here are few examples of using these access modes in Python:
f = open("example.txt") # Uses default access mode 'r' f = open("example.txt",'w') # Opens the file for writing in text mode f = open("img.bmp",'r+b') # Opens the file for reading and writing in binary mode
The With Statement
Besides having a cleaner syntax, the "with" statement also makes exception handling easier when working with file objects. It is considered best practice to use the with statement when working with files whenever applicable.
A significant advantage that using the with statement offers is that it automatically closes any files you've opened after the operation is complete. Thus, you don't have to worry about closing the file and cleaning up after performing any operation on a file.
The syntax of the with statement is:
with open("File_Name") as file:
To read a file using the with statement, you would write the following lines of code:
with open("example.txt") as f: data = f.readlines()
When the second line of code runs, all of the data stored in "example.txt" will be stored in a string called "data."
If you wanted to write data to the same file, you would run the following code:
with open("example.txt", "w") as f: f.write("Hello World")
Reading a file in Python is straightforward – you must use the 'r' parameter or not mention a parameter at all since it is the default access mode.
That said, there are other ways to read data from a file in Python. For example, you could use the read(size) method built into Python to read the specified size of data. If you do not specify the size parameter, the method will read the file until its end.
Let's assume that "example.txt" has the following text:
This file has two lines.
To read the file using the read() method, you would use the following code:
f = open("example.txt",'r',encoding = 'utf-8') f.read(5) # reads the first five characters of data 'Hello' f.read(7) # reads the next seven characters in the file ' world.' f.read() # reads the rest till EOF '\nThis file has two lines.' f.read() # returns empty string
In the example above, the read() method returns the new line character when the line ends. Furthermore, once the end of the file is reached, it returns an empty string.
It is possible to change the cursor position regardless of what access mode you're using with the help of the seek() method. To check the current position of the cursor, you can use the tell() function.
You can also read files line-by-line using a for loop, like so:
for line in f: print(line, end = '') ... Hello world. This file has two lines.
The readline() and readlines() methods are two other reliable techniques to read data from files. The readline() method stops reading the line after it reads a new line character.
f.readline() # reads the first line 'Hello world.'
On the other hand, readlines() reads the file until the EOF is reached.
f.readlines() # reads the first line 'Hello world.'
Appending New Text
To write to a Python file, you must open it in the write (w), append (a), or exclusive creation (x) mode.
You must be careful when using the write mode since it overwrites the data in the file if it already exists.
To write to a file regardless of whether it's a text or a binary file, you can use Python's write() method.
with open("example.txt",'w',encoding = 'utf-8') as f: f.write("Hello world.\n\n") f.write("This file has two lines.")
If "example.txt" does not exist in the directory, it will be created with the specified text written to it. On the other hand, if the file exists in the same directory as the script, the data previously held by the file will be removed and over-written with the new text.
Closing a File
You must close the file after reading from or writing to it. It's the only way to release the resources taken up by the file.
Closing a file in Python is easy – all you have to do is use the close() method. Running the method frees up the memory taken up by the file. The syntax is:
To close "example.txt" after reading and writing, you must simply run the following code to close it:
The close() method can sometimes throw an exception, leading to the code exiting without the file closing.
A better way to close a file after operations is to use the try…finally block.
try: f = open("example.txt", encoding = 'utf-8') # perform operations finally: f.close()
Using the try… finally block ensures that open files are closed correctly regardless of whether an exception is raised or not.
Learning file handling and learning to copy/move files and directories will help you master working with files in Python.
Next, check out our easy tutorial on copying/moving files with a progress bar using Python.