Table of Contents
You might be familiar with the Python programming language’s common traits, like it being general-purpose, high-level, and interpreted. However, the emphasis that stuck out with this programming language’s design philosophy is its code readability. When interacting with Python in a programming environment, you will find yourself using many white spaces to make its code as clear and direct as possible for anyone to comprehend what it is trying to achieve.
When we try to figure out how Python works with files, the first step to cracking this puzzle is using your mind to simulate the general file operations you perform each time you sign-in to your operating system. Such file actions include creating, opening, reading, writing, copying, and merging files. These file operations are easily achievable through the computer mouse and keyboard. However, we can also use the Python programming language to achieve these file operations.
Why Work with Files in Python?
You might be asking yourself, why not stick to the computer mouse and keyboard approach of handling files? To answer this question, we will use a theoretical yet practical approach. In a programmer’s world, file handling is a means to many ends. For instance, you might be working on a Python program that needs to store data. You could say that a database will be sufficient enough to store this data.
However, the data you need to store is not yet ready to enter the database. Some additional operations needed to take place on the data instance before we move it to a database. Therefore file handling will give you the platform of playing with or manipulating your data instance through various file operations.
One advantage of Python when dealing with files is that you do not need the assistance of third party libraries to execute your file handling operations. It provides an inbuilt function to cater to the file operations that you need whether you want to create, write, or read a file.
Opening a Text File in Python
Python’s built-in open () is responsible for opening a file. This open function is assigned to a file object function, as we shall see through its syntax. Therefore, the various operations you need to perform on a file are dependent on this file object’s declared methods and attributes. The syntax for this operation is as follows:
file_object = open ("filename", "mode")
The filename attribute relates to the file’s name we are trying to open or is already opened through the file_object.
The mode attribute informs the file_object of the filename’s assigned mode. This mode attribute is pretty straight forward as we will unravel its working mechanics and the traits associated with it in the following section.
Creating a Text File
We can look at how a Python file_object functionality can enable us to create a text file. Take a look at the following demonstration. Let us assume we want to create a file called code.txt.
f = open("code.txt", "w+")
The declared variable f is the file_object that will open our code.txt file. Two arguments are mandatory when opening a file with a Python function. The first argument is the filename we will be working on, and the second argument is the operable permissions we want to associate with the named file. These operable permissions relate to the mode we discussed earlier.
Therefore, analyzing the w+ mode we used in this function yields interesting outcomes. The w portion of the argument indicates that we should write or create the named file. The w portion of the argument is also a safety net for the f function and also indicates that we should proceed to create the named file if, by chance, it does not exist. The + portion of this second argument indicates that the write operation the f function intends to performs needs to be both read and write. Read and write imply that we can create and edit a file where necessary.
for z in range(15): f.write("My name is line %d\r\n" % (z+1))
We begin this step by creating a for loop whose execution should produce an output range of 15 numbers. Since we already have a code.txt file created, we will use the write function to feed data into this code.txt file. We expect this function to iterate an output that will be written in the code.txt file. The expected output is the phrase “My name is line,” followed by a printed line number that will range from 1 to 15. We used the %d sign because we want to display these numbers as integers.
The carriage return \r and the new line \n contribution is also straightforward. The new line will take us to a new line every time an instance of the for loop executes, and the carriage return will create a hard line after each instance of the for loop executes successfully. Think of the carriage return as the keyboard’s Enter key. In this case, every time the phrase “My name is line ” is printed, we assume we pressed the keyboard’s Enter key until the condition of the for loop is fulfilled. Even when the last phrase under the for loop is met, the carriage return will take the cursor to a new line.
After we are done with the read and write operations on our code.txt file, its only fair that we exit the function professionally. Therefore the f.close() function will end the instance of the stored code.txt file.
How about we take a coffee break from this rocket science theory and practically try to achieve the objectives we have discussed so far?
Open your code editor and create a file called pythonfiles.py and populate it with content similar to the one depicted below. Before you save this file, create a custom directory with a name like python_files_tutorials and save the file pythonfiles.py inside it.
def main(): f = open("code.txt", "w+") for z in range(15): f.write("My name is line %d\r\n" % (z+1)) f.close() if __name__ == "__main__": main()
The next step is to open your OS terminal from within this directory. For the case with my Ubuntu OS, following the discussed guidelines should have it looking like this:
Since within this terminal instance, we have the pythonfiles.py file when we run the ls command, we want to make sure that the file can execute from the terminal. To give the file the needed execution permissions, do the following:
tuts@CodeUnderScored:~ python_files_tutorials$ chmod +x pythonfiles.py
If you run into permission errors with this command, consider using the Sudo command but make sure you are a sudoer user or have Sudo privileges.
tuts@CodeUnderScored:~ python_files_tutorials$ sudo chmod +x pythonfiles.py
Now that our pythonfiles.py file is executable, we can run our code through the following command. Make sure you have Python 3 installed on your system.
tuts@CodeUnderScored:~ python_files_tutorials$ python3 ./pythonfiles.py
If an empty instance returns on your terminal once you run the above command, everything is okay. You can now open the python_files_tutorials directory, and you should be able to see a newly created code.txt file that was not there in the first place. The content of this text document should be similar to the screenshot below.
Appending Data on a Text File
To append data to a target file is a fancy way of saying that we want that file to have additional content to the one already in it. Therefore, appending data is the same as adding data. However, the append file operation is also applicable to creating a new file if the target file does not exist.
f = open("code.txt", "a+")
The above file object has both the filename and append mode (a+) as attributes. The plus sign after append (a) tells the file object to create a new file with the specified name if it does not exist. However, since we already created the code.txt file and populated it with some data, there is no need for a new one, and therefore the object file will ignore the need to create a new file.
for y in range (3): f.write("I am appended line %d\r\n" % (y+1))
In the same python_files_tutorials directory, create another python file and name it append_pythonfiles.py. The content of this file should be similar to the following snippet. You can mock the earlier steps to make your file executable and then run the python file from your terminal.
def main(): f = open("code.txt", "a+") for y in range(3): f.write("I am appended line %d\r\n" % (y+1)) f.close() if __name__ == "__main__": main()
tuts@CodeUnderScored:~ python_files_tutorials$ chmod +x append_pythonfiles.py
You can optionally use Sudo with the above command in case you run into any permission issues.
tuts@CodeUnderScored:~ python_files_tutorials$ python3 ./append_pythonfiles.py
Hit your keyboard’s Enter button. If you have an empty terminal instance, then everything is executed correctly. Open your code.txt file and check for any additional modifications. The content of the file should be similar to the screenshot below.
Reading a File in Python
You can use Python to successfully read a file by specifying the filename and its extension together with the mode in which it should be read. Let us consider a more practical approach.
f = open("code.txt", "r")
Here, we are opening the code.txt file in read mode. An example instance of a read mode is like opening a file in a pdf format. We only have the privilege of accessing the contents of the file, but we cannot manipulate or edit the visible content.
if f.mode == 'r':
Here, the mode function is useful in our conditional if statement. It checks if indeed the code.txt file we want to open should be in read mode.
Now that we have successfully performed a conditional check on the mode we want to open and read our code.txt file, the next step will be to declare a variable and assign it to the read() function that will be holding the read file data.
content = f.read()
Print the output of the file.
To achieve all these discussed steps, create a new Python file in the python_files_tutorials folder and name it read_pythonfiles.py. Populate it with the code similar to the snippet below. The required steps to run this code are obvious.
def main(): f = open("code.txt", "r") if f.mode == "r": content = f.read() print(content) if __name__ == "__main__": main()
tuts@CodeUnderScored:~ python_files_tutorials$ chmod +x read_pythonfiles.py
tuts@CodeUnderScored:~ python_files_tutorials$ sudo chmod +x read_pythonfiles.py
We can now execute our read_pythonfiles.py file and see what happens.
tuts@CodeUnderScored:~ python_files_tutorials$ python3 ./read_pythonfiles.py
Your terminal should provide a similar output to the screenshot below.
Using Python to Read a File Line-by-Line
The previous step showed us how to read the whole content of a file. Supposing a file is too big, and we still want to browse through all the content in it? We cannot rely on a function that dumps the output of a whole file content at once. There is a special function to achieve this objective for us, and its name is readlines(). Let us create a Python file called readlines_pythonfiles.py and implement this code functionality we are trying to achieve. You should populate your Python file to be similar to the snippet below.
def main(): f = open ("code.txt", "r") l1 = f.readlines() for w in l1: print(w) if __name__ == "__main__": main()
When we execute this readlines_pythonfiles.py file from our terminal, we will get a similar output to when we executed the read_pythonfiles.py file. However, you will note the output spacing after running readlines_pythonfiles.py is broader. The code segment l1=f.readlines() reads the content of the text document line by line. After each successful line by line read, it presents the read lines separately in a readable format.
The effective applicability of this readlines() function is when dealing with complex data files. If you are dealing with non-readable file content, this function will give you a broader perspective regarding the content on every line of that file. Therefore, we can generalize that the main difference between readlines() and read() is that read() will give you a general view of a file. In contrast, readlines() will give you an in-depth perspective regarding the same file without leaving out any detail.
Python File Modes
You are bound to run into the following file modes in your advanced stages when dealing with Python files. Let us have a look at these modes and their various descriptions.
‘r’ – It is the most common or default mode in Python, and it will help you open and read files under a python program.
‘w’ – We use this mode to open and write data in a file. If the target file does not exist, a new one is created. If the target file exists, it is truncated.
‘x’ – We use this mode to create a new file. The operation will fail if the file being created already exists.
‘a’ – We use this mode to add new content to a file. If the target file does not exist, a new file will be created before the operation continues.
‘t’ – We use this default mode when we want to open a file in text mode.
‘b’ – We use this mode when we want to open a file in binary mode. The binary mode is a machine language mode.
‘+’ – We use this mode when we want to update the content of a file. It entails both the read and write operations of a file.
We have achieved some helpful and insightful knowledge base in regards to working with Python files 101. We can summarize our achieved learning credentials into the following bullet points.
- Through Python, you should read, write (create), and append (add) files.
- The Python function, open(“file_name”, “w+”), will help you create a file after you specify the name of the file_name together with its extension. The + sign specifies that the file needs to be in a read and write mode.
- The Python function, open(“file_name”, “a+”), will help you add or append data to an existing file. If the file does not exist, it will create it before appending the needed data.
- To output all the content of an existing file, use the read() function.
- To output all the content of a file but line by line in a more detailed manner, use the readlines() function.
If you can build upon this basic knowledge base and code fragments concerning how you interact with files through Python, you will achieve a lot when creating dynamic applications. For instance, an e-commerce project will require invoicing and receipting operations. You will be able to generate files that mock the dynamic nature of such applications.