
Python is often referred to as a simple and fast automating programming language. It is quite handy in automating many tedious and boring day to day tasks.
Reading and writing files is a very common task that you would come across many times during your Python programming journey. Hence, having a clear understanding of it is very crucial.
Working on a file involves 3 major operations:
- Opening the file
- Reading/Writing to the file (or both)
- Closing the file
Let’s turn experimental to better understand these operations. Fire up your Python shell by typing:
>> python
1. Opening a File
We will now create a file named ‘file.txt’
>> file = open('file.txt', 'w')
The above command opens the file in write mode, if it already exists or else it create a new file with the name ‘file.txt’ and then opens it in write mode.
2. Reading/Writing
Python allows us to open a file in any one of the 3 file modes. These can be broken down with their meanings as follows:
| File Mode | Meaning |
| ‘r’ | Opens a file in read-only mode i.e you can only read file contents. |
| ‘w’ | Opens a file in write-only mode i.e you can only modify file contents. |
| ‘a’ | Opens a file in append mode i.e you can add new content to the existing content of the file. |
| ‘rb’ or ‘wb’ | Opens a file in respective ‘r’ or ‘w’ mode but in the binary mode. |
The ‘file.txt’ above has been opened in ‘w’ i.e write-only mode. Let’s add some content to it:
>> file.write('Hello World\n')
This command writes a line ‘Hello World’ to the file. We can read the same by opening the file in read mode and then reading it.
>> file = open('file1.txt', 'r')
>> file.read()
3. Closing the file
It is always a good practice to close a file after you are done performing operations on it. Here are some prominent reasons why you should consider closing your files:
- Keeping many files open consumes your RAM space rapidly, hence slowing down your program.
- The changes you made to a file are generally not saved unless you explicitly close it. However, Python automatically tries to close files for you as a part of the garbage collection process.
- If you keep a file open other programs are not allowed to open it.
To simply close the file we created above just type:
>> file.close()
File Manipulation Examples
1. Copy contents of one file to another
>> file1 = open('file1.txt', 'r') #File to copy
>> file2 = open('file2.txt', 'w') #Destination file
>>
>> for line in file1.readlines():
... file2.write(line)
...
>> file1.close()
>> file2.close()
2. Append content to a file
>> file = open('file1.txt', 'a')
>> text_to_append = input("Enter text to append: ")
>>
>> file.write('New Hello World\n')
>> file.close()
Handy Tip: If you often forget to close the opened files you should open you files as follows:
>> with open('file.txt', 'r') as f:
... print(f.read())
...
>>
The with block automatically closes the file after it finishes so you don’t have to bother explicitly to close it.
File manipulation with Python is extremely easy and powerful. Mastering it is a great step towards boring tasks automation and greatly helps save a lot of time.
Feel free to drop a comment below to let me know how you use this power feature for automating your tasks or if you found this post helpful.