One of the mandatory skills for any programmer to own is file handling. File handling is required to make a file, move a file, delete a file, etc. It is supported by Python and allows us to handle files, i.e, writing files, reading files, operating on files, etc.
There are different modes in opening files. The way we choose the mode is by deciding the way we would like to use the file or the kind of data we would like to read (or write) from (or to) the file. However, we have to specify the mode we would like to use when opening the file with the in-built method
There are various file modes we are able to use:
r: opens a file for the sole purpose of reading it.
r+: used if we want to read the information/data or write the information/data.
rb: opens a file in binary format for the sole purpose of reading it.
a: opens a file and allows us to append data at the end of it.
a+: will be used if we want to read data from the file or append data into the file.
wb: opens a file to write in binary format.
wb+: opens a file to read and write down in binary format.
w: opens a file to write or it creates a file if the file hasn’t already been created.
w+: opens a file to write (creates a file if it doesn’t exist) and read. If the file already exists, it will overwrite it.
+: means “reading” or “writing” and it is employed with
file_object = open(file.name, mode)
Within the syntax, the file’s location or the name of the file that we would like to open is
file.name – this
file.name should have its extension specified.
file1 = open("C:/Desktop/PythFiles/sample.txt", "r+")
In this example, we are opening a file named
sample.txt ,which is within
C:/Desktop/PythFiles/, in read and write mode.
file1 = open("C:/Desktop/PythFiles/sample.txt", "rb+")
In this example, the
sample.txt, which is within C:/Desktop/PythFiles/`, is opened in the binary format.
After we open a file, we are going to ultimately close it, this can be done using the
close() method. After we close the file, we cannot read or write within the code unless we open it again.
shutil.move method is used to move files. It allows us to transfer a file/folder from one folder to a different folder.
The above line of code should be inserted at the beginning of the program so that we can access the
This function can take two parameters, the
source and the
To move our
sample.txt file to a different directory (called Python),
we can write the subsequent code:
import shutil source = "sample.txt" destination = "Python" new_path = shutil.move(source, destination) print(new_path)
The code returns:
os module has some methods that we can use to delete or clear a file and an empty directory.
remove() method can be used to delete files. It takes the file’s location as a parameter.
import os if os.path.isfile('C:/Desktop/PythFiles/sample.txt'): os.remove('C:/Desktop/PythFiles/sample.txt'): print("Done") else: print('The file doesnt exist')
os module includes a method called
rmdir() that helps us delete or clear an empty directory.
import os os.rmdir('directory')
shutil can be used to delete a file or perhaps an entire directory. For this, we use the method
rmtree(), which removes the information or data using recursion.
import shutil shutil.rmtree('/PythFiles/')
In the example above, the “PythFiles” directory is deleted or removed along with all of its contents.
pathlib module is a built-in module that we can use to delete a file or an empty directory.
import pathlib file = pathlib.path("PythFiles/sample.txt") file.unlink()
In the example above, we are using the
unlink() method to remove the file from the mentioned path and the
path() method to get the file’s path.
View all Courses