File Handling in Python - Part 2

Kicking off from where we stopped in part 1. Before we move further let's have a quick recap of what we did in part 1 HERE. We know what file is, how to create content, how to append a file, and how to read from a file. So in this article, we will be discussing:

  • Other access modes

  • Closing file in python

  • Rename and Remove method

  • Conclusion

Other access modes

Remember we said access modes are the second argument to pass in the open function. It will define what operation you want to do on a file. Simply put, it will tell python as whether you want to open a file in:

  • Write mode

  • Read mode

  • Both read and write mode

Although a lot of other access modes exist. They are:

'r+' - Read and Write Mode

'w+' - Write and Read Mode

'a+' - Append and Read Mode

'r+' - Read and Write Mode

This mode is used to open a file for reading and writing. The file pointer is placed at the beginning of the file.

'w+' - Write and Read Mode

This mode is the same as write mode but it also allows us to read from a file.

'a+' - Append and Read Mode

This mode is the same as Append mode but it also allows us to read from a file.

me = ["i am strong. \n", "i am confident. \n", "i am gifted. \n"]
fh = open("me.txt", "a+")

the result is shown in the snippet below

i am incredible.
i am strong. 
i am confident. 
i am gifted.

Note: Here the list added, appended to the existing data in the file.

Note: The modes mentioned above are for opening, writing, or reading text files only.

Binary Files

While using binary files, the same modes will be used with the letter 'b' at the end. This way python will realize that we are interacting with binary files.

Access mode in binary files are listed below:

'wb' - Open a file for write-only mode in the binary form.

'rb' - Open a file for read-only mode in the binary form.

'ab' - Open a file for append-only mode in the binary form.

'rb+' - Open a file for read and write mode in the binary form.

'ab+' - Open a file for appending and read-only mode in the binary form.

Closing files in Python

The file needs to be closed when we are done performing operations on the file, it needs to be properly closed. It is done by the close function.


Note: Any other argument passed after the file has been closed will result in an error.

C:\Users\TINUKE\PycharmProjects\shecodeafrica\venv\Scripts\python.exe "C:/Users/TINUKE/PycharmProjects/shecodeafrica/file"
Traceback (most recent call last):
  File "C:/Users/TINUKE/PycharmProjects/shecodeafrica/file", line 13, in <module>
ValueError: I/O operation on closed file.

Process finished with exit code 1

Python Rename or Delete File Python provides us with an “os” module which has some in-built methods that would help us in performing the file operations such as renaming and deleting the file.

In order to use this module, first of all, we need to import the “os” module in our program and then call the related methods.

Rename() method

This rename() method accepts two arguments i.e. the current file name and the new file name.

os.rename(current_file_name, new_file_name)


import os
os.rename("C:\\Users\\TINUKE\\PycharmProjects\\shecodeafrica\\me.txt", "C:\\Users\\TINUKE\\PycharmProjects\\shecodeafrica\\bami.txt")

Now the name has changed from this me1.PNG to this bamitxt.PNG You will notice that the name of the file has changed without the content changing.

Remove() method

We use the remove() method to delete the file by supplying the file name or the file location that you want to delete.

import os

After you run the above command you will notice that bami.txt no longer exists.

Python handles files in various ways, I hope we now understand how python handles files. You have done well. If you read to this point you deserve a thumbs up. well done.

Thank you for reading.

To read more on file handing in python you can click here or here