File Handling in Python

Beginners Code Computer Skills Python

File Handling

File Handling refers to the manipulation of files using a set of commands. Till now we have been working with files by giving them direct input. Now we will learn how to open and close files for security purposes and thus control the editing access. We have other functions which include restricting access, renaming, deleting and much more.

We will learn only the basic functions in file handling. This will give you an easier grip to understand it.

open() function

This function opens up the file with the desired name and it can read and store the input provided. Following example will make it clear.

sampfil = open("sample.txt", "w+")
print("name of file", sampfil.name)
print("Closed or not", sampfil.closed)
print("Mode of opening", sampfil.mode)

here we create a sample.txt file and open it in w+ mode. This mode means that we can both read and write into the file. Some other modes example includes:

ModeDescription
rFile is possible for reading only format. Default format.
wFile is possible for writing only format.
r+ OR w+File is possible for reading and writing format.
rb OR wbSame functionality but in binary input.
aFile is possible for appending only format.
a+File is possible for appending and reading format.

close() function

This function closes the file and blocks any further mode of writing into the file. The syntax is as follows:

filename.close()

write() function

This function allows us to write into the open files only. If we place it after a close() function then it might give us an error.

Compile this code to understand in a better way:

fo = open("foo.txt", "w+")
print("name of file", fo.name)
print("Closed or not", fo.closed)
print("Mode of opening", fo.mode)

fo.write("I am Tony Stark. I am Iron Man. I am the wittiest Avenger")
fo.close()
fo.write("I am Bruce Banner. I am the strongest Avenger")

Here we close the file and then we have again given a write function input. Thus we get an error here.

rename() function

The rename facility is provided by the python os module. It means that to use rename(), we need to import the os module first.

The syntax is:

import os
os.rename("tony.txt", "hulk.txt")

remove() function

The rename facility is also provided by the python os module. The syntax is as follows:

import os
os.rename("hulk.txt")

mkdir() function

This function is used to make new directories. The name itself suggests “make directory” as mkdir. This function is provided by the os module. The syntax is as follows:

import os
os.mkdir("Avengers")

chdir() function

This function is used to change the current directory. Hence it is called as chdir() or “change directory”. The syntax for it is as follows:

import os
os.chdir("Avengers")

rmdir() function

This function is used to delete/remove the directories. The syntax is as follows:

import os
os.rmdir("Avengers")

These were some of the basic functions of file handling. As you can see, these basic functions allow us to have more control over the file in which we compose the code. Thus it ensures the security and privacy of a user file while simultaneously giving a wide range of editing access over a file.