Introduction to file handling of python
In this tutorial, you are going to learn about the file handling of python. Using file handling, we can store data permanently in your hard disk. There are two types of file:
- Text file: It contains the human-readable characters.
- Binary file: It contains the executable instruction.
Modes of a file in Python:
Generally, in python there are a total of 8 modes of file:-
- r : It will read the file only when the file exists.
- r+ : It will read and write the file only when the file exists.
- w : This mode will write the file in both the condition, i.e., the file exists or the file not exist. If the file exist then it will write to that file. In the non exists the case of the file, it will create the one and then write in it.
- w+ : This mode will read and write the file in both the condition of file exist or not same as the w mode.
- a : It will write the file in append mode when the file exists or it does not exist. If the file exists, then it will append in that file at the end of the file. In the non exist case of the file, it will create the one and then write in it.
- a+: It will read and write the file in append mode in both the condition of file exist or not the same as the a mode.
- x: This mode will write the file only when the file will not exist as it will create the one and write in it.
- x+: This mode will read and write the file in the same condition as the x mode.
Note: The default type of mode is t (i.e., text file) which means that the mode r, r+, w, …etc will read the text file only. The binary file can be read by adding b in the mode. For example: rb, rb+, wb, wb+, ….etc.
Steps used in Python for file handling in Python
- Open the file in a specific mode.
file=open(file path, mode and file type)
- Perform read or write operations.
file.read() or file.write()
- Close the file.
Different operations in file handling in Python
- How to create or open a file in Python
The mode w will create the text file ‘demo’ if the file will not exist. Otherwise, if the file exists, then it will open the ‘demo’ file.
# it will create the text file oj=open('demo.txt','w') print("Created the file.") oj.close()
Created the file.
Note: The demo file will be saved in that location where your program has been saved. For example, My programs will save to the desktop. So, my demo file will be created and saved on the desktop.
- How to read data, write data and close the file in Python
Open the file demo in w+ mode. Write the file demo and the pointer goes at the end of the line after the completion of write operation. Get the pointer at the zeroth position by using seek() method. Read the data from the file. Close the file.
# Open the file oj2=open('demo.txt','w+') # Write the file oj2.write(" This is the text file.") # to get the pointer at the zeroth position oj2.seek(0) # Read the file print(oj2.read()) # Close the file oj2.close()
This is the text file.
- To create a directory in Python
The mkdir() method of the os module will create a directory in the current directory. You need to supply an argument to this method which contains the name of the directory to be created.
import os # this will create the folder os.mkdir("Python")
- To remove directory in Python
rmdir() will remove the directory. Pass the name of the directory which you want to remove as the argument of the rmdir().
Note: rmdir() will remove the directory only when it is empty ( it doesn’t contain any file).
import os # to remove the directory os.rmdir("Python")
- How to rename a file in Python
rename() will change the name of the existing file.
Syntax: os.rename(existing_file_name, new_file_name)
import os os.rename("demo.txt","blog.txt")
- How to walk in folder, sub-folder and files in Python
os.walk() will walk through your current directory and return the path to the folder, subfolders and files.
import os for folder, subfolder, files in os.walk(os.getcwd()): print(folder) print(subfolder) print(files)
- How to move a file
How to move a file from one directory to another in Python
- To delete a file
How to delete a file in Python with examples