Python is an amazing programming language for most tasks, such as web development, AI, automation, or general programming. Working with files and folders is important because we have to use them to automate tasks or store data and various other similar tasks.
To work with special format files like image files, JSON files, PDF, CSV files, or database files, python has amazing modules written by great programmers to make our task easy. You can check our step by step tutorial on working with SQL database files using python by which you can learn SQLite, which is a great way to store and retrieve data of python programs.
Before we begin, we need to have python install in the system first. If you don’t have it installed, you can see our guide on installing python.
Working with Files in Python
Files are an important part of our everyday work. We use PDF files, Excel files, or normal text files for many tasks. The tutorials you are reading on this website are in an HTML file that your browser processes. Like many other programming languages, Python also supports File handling.
Opening and Closing Files
We can easily open a file in python using the built-in open() function. The open function accepts many arguments, but the main required argument is the path to the file that we want to open. The open() function has a single return, which is the fileobject.
file_obj = open("filename.txt")
The code will open the file named “filename.txt,” a text file present in the same folder. Then it will store the return fileobject in the file_obj variable.
When we have all the processing done with the file, we need to remember the closing of the file. A file is closed after the program is terminated in many cases, but it is good to close it using the close() function whenever we don’t need it. If we forget to close a file, it will simply consume memory, which will slow down the program. It is considered a bad practice for bigger projects.
To close an opened file, we need to use the close() function of the file object.
# here we will open the file filename.txt file_obj = open("filename.txt") print("The file has been opened successfully...") # Now we can do processing with the file # After processing we need to close the file file_obj.close() print("The file has been closed...")
We opened a file named filename.txt in the same folder using the open() function and then closed it using the close() function.
Output:
Though the close() function is useful in closing a file, there is one more way to close a file, i.e., by opening the file using the with the statement.
Example:
with open("filename.txt") as file_obj: # Here goes the file operations
The code will automatically close the file once we get out of the with block. The with statement also closes the file if there will be any error so, it is better to use the with the statement as we can close the file and handle the file errors easily using it.
File Opening modes
Though the open() function can be used with only one argument, i.e., the file path, we can also use another argument named mode. It signifies the mode which is used to open the file.
For example, if we want to open the file for only reading and don’t want to edit it, we can use the ‘r’ mode as an argument to open() function, which means read-only mode. This is also the default mode for the open() function. For writing or modifying a file, we need to open the file with write mode using the ‘w’ as an argument.
Example:
with open("filename.txt", w"): # Here goes the statements # to perform on the file
This code will open the file in write mode so you can perform write operations on the file.
There may also be situations where we want to open a file in binary mode to perform some operation on it. To do that, we have to mode ‘rb’ for reading in binary mode and ‘wb’ for writing in binary mode.
Example:
with open("filename.dat", "rb"): # Here goes the statements # to perform on the binary file
It will open a data file named filename.dat in binary mode for only reading.
Till now, we have learned how to open a file and close a file. Now let us see how we can read data from the file using Python.
Reading Files Using Python
There are many ways in which we can read data from an opened file in python, but the most common way is by using the read(), readline(), and readlines() functions.
The read() function
It accepts the number of bytes to be read from the file as an argument and reads that amount of bytes from the file.
If we don’t provide any argument or use None or -1 as an argument to the read() function, then the entire file will be read in the read-only mode.
Example:
with open("filename.txt") as file_obj: # using the read() function to read bytes # from the file object words = file_obj.read() print(words)
Output:
You may need to create the sample file filename.txt with demo content or specify other files in the argument of open() function before running the program; else, python will throw FileNotFoundError as shown below.
Traceback (most recent call last): File "file.py", line 1, in with open("filename.txt") as file_obj: FileNotFoundError: [Errno 2] No such file or directory: 'filename.txt'
The read() function is an amazing way to read bytes from the file, but there are more ways to read data from a file. Mainly there are two other methods to read data from a file. They are the readline() and readlines() methods.
The readline() Function
The readline() function is used to read a line at a time. Every time we run it, we will get the next line of the file.
Example:
with open("filename.txt","r") as fileobj: print(fileobj.readline()) # print the first line print(fileobj.readline()) # print the second line
Output: We will get the first and the second line of the file printed.
We can also pass the number of characters to read from a line as an argument to the readline() function.
Example:
with open("filename.txt","r") as fileobj: print(fileobj.readline(15)) # print the first 15 bytes
Output:
The readlines() function
The readlines() function is used to read all the lines of a file. This function will return a list of all the lines present in the file. If we don’t need to run all the lines, then we can also specify the number of lines we need as an argument.
Example:
with open("filename.txt","r") as fileobj: print(fileobj.readlines()) # return a list of all the lines
Output:
Writing Files Using Python
To write data in a file using python, we need to use the write() method of the file object. The write function accepts the data as an argument that we want to write in the file.
Example:
line = "This is a new line\n" with open("filename.txt","w") as fileobj: print(fileobj.write(line))
On running the code, all the filename.txt data will be replaced by the string “This is a new line”. If you don’t have the filename.txt file present previously, then it will create one.
Appending Files
While writing a file as we did in the previous code, you may notice that the new data replaces all the previous data present in the file. Sometimes we are only required to append the new data instead of rewriting the data. We need to open the file using the “a” mode and then use the write() function to write the data.
Example:
line = "\nThis is a new line" with open("filename.txt","a") as fileobj: print(fileobj.write(line))
We will have a new line written in the file filename.txt without replacing the previous lines on running the code.
Renaming and Deleting Files
To rename a file, we need to use the rename() function of the os module. The rename function accepts two important arguments, the first argument is the path to the file we want to rename, and the other argument is the new name of the original file.
Example:
import os os.rename("file1.txt,file2.txt")
The code will rename the file file1.txt with the name file2.txt.
To delete a file using python, we need to use the remove() function of the os module.
Example:
import os os.remove("sample.txt")
This will delete the file sample.txt present in the current directory. You can also give the file’s path if it is present in other directories. If the file does not exist in the given path, then we will get a FileNotFoundError. Use the exception handling method outlined in the previous sub-topic to deal with errors.
Conclusion
In this tutorial, we have learned everything necessary to work with files in python, such as creating files, reading data from files, writing data to files, removing and renaming files, etc.
You can also refer to our step by step guide on working with SQLite database on python, where you can learn everything you need to know while working with SQLite databases in python.