File Handling in Python
In this section, we will learn about file handling. File handling in python is an important module where users can interact with files directly starting from reading, writing, deleting to storing in the disk.
Open a file :
Python provides a built-in open() function that can be used to open a file. This accepts mainly two arguments and returns a file object. The following example is the syntax to open a file.
file = open('filepath','mode')
‘mode’ option specifies to open the file in ‘read’,’ write’ or ‘append’ mode.
- ‘r’ mode is used to open the file in reading mode.
- ‘w’ mode is used to open the file in write mode.
- ‘a’ mode is used to open the file in append mode.
- ‘b’ mode is used to open in binary mode.
The following example is to open a file in reading mode:
file = open('sample.txt','r') print(file.read())
To open a file in write mode:
file = open('sample.txt','w')
To open a file in both append mode:
file = open('sample.txt','a')
To read an encoded file ‘encoding’ argument is used.
file = open('sample.txt',mode = 'r', encoding='utf-8')
At times the user may need to read only the first line or the second line. In such a case, ‘readline’ function is used.
file = open('sample.txt','r') print(file.readline())
To read all the lines:
The loop method can also be used to return all the lines in an efficient manner. The following example shows the use of the loop method to get all lines.
file = open('sample.txt','r') for line in file: print(line)
Close a file:
File object needs to be closed after the operations are complete. Closing the file will free up the resources.
file = open('sample.txt','r') file.close()
Write a file:
Similar to the read function, the open() function is required to open with ‘w’ mode to write to a file.
To write/append to an existing file:
file = open('sample.txt','w') file.write("added first line") file.close()
file = open('sample.txt','a') file.write("appending a new line") file.close()
To create a new file ‘x’ is used as a mode.
file = open(newfile.txt','x')
Follow the below steps to write multiple lines.
with open("sample.txt",'w') as file: file.write("Hi all,Welcome to DataUnbox.com \n") file.write("Enjoy learning python and data science\n") file.close()
Delete a file:
To delete a file user has to import the OS library. Deleting a file will delete the file from the disk.
import os os.remove("sample.txt")
This will check the file in the current default directory. If you wish to delete a file that is not in the default path, then you have to provide the full path of the file.
To remove a directory:
This folder has to be empty to be able to delete by python.
To check if the directory is empty or not:
This will print all the folders and files. In case of an empty directory, this will return an empty list.
In this section, you learned about file handling in Python. File handling in Python is a vital module from a programmer’s perspective as you may have to work with files daily basis. In the next section, we will understand the errors and exceptions.