- What is File Handling In Python?
- Common functions used in file handling in python:
- Common access modes of python file handling:
- Example 1: Creating a text file
- Output:
- Example 2: Writing a file
- Output
- Example 3: Reading a file
- Output
- Example 4 : Appending a file
- Output
- Other access modes for python file handling:
- Methods used with file positions:
- tell()
- seek()
- More file operations
- Directory operations
What is File Handling In Python?
File Handling In Python– Python is generally preferred for its simplicity. Similarly, file handling concepts that are tougher in many other object-oriented languages is also made easy in python. Python files can be written both in the text as well as binary.
Download notes on file handling in python
The general syntax of working with files:
Syntax:
file_object = open(filename,access_mode)
Explanation
file_object – indicates the object created for the file
open() – this method opens the file and performs the operation based on the access_mode
filename – the file which needs to be manipulated
access_mode – specifies the mode of operation such as read, write or append
Common functions used in file handling in python:
There are 4 common functions used in python file handling without which any operations in files cannot be performed. They are:
- read() – reads the file that has been passed to the file object
- write() – writes the content specified to the file
- open() – opens the file
- close() – closes the file that has been opened. It is always a good practice to close the file at the end.
Common access modes of python file handling:
There are 4 common modes of operation for file handing in python:
- x– indicates the creation of file
- r– indicates that the file has to be read
- w– indicates to write contents in the file. If the file already exists, then it just overwrites the file with the current contents.
- a– indicates that contents need to be appended. The file pointer will be at the end of the file. If the specified file is not present then it creates a new file and the file pointer will be at the start of the file.
Example 1: Creating a text file
f=open("sample.txt","x")
Output:
A sample.txt file will be created in the folder the python file is located
Example 2: Writing a file
f=open("sample.txt","w") f.write("File handling in python") f.close()
Output
The contents are written in the sample.txt file
Output in notepad–
Example 3: Reading a file
f=open("sample.txt","r") print(f.read()) f.close()
Output
File handling in python
Example 4 : Appending a file
f=open("sample.txt","a") f.write("… Happy learning….") f.close()
Output
Updated data in sample.txt will be==> File handling in python… Happy learning….
Other access modes for python file handling:
- rb – The file is read in binary format.
- r+ – the file can be used for both reading and writing contents in text format
- rb+ – the file can be used for both reading and writing contents in binary format
- wb – the file can be written in binary format
- w+ – the file can be used for both reading and writing in text format
- wb+ – the file can be used for both reading and writing in binary format
- ab – the file contents can be appended in binary format
- a+ – the file can be used for both reading and appending contents in text format
- ab+ – the file can be used for both reading and appending contents in binary format
Methods used with file positions:
There are 2 file methods that are used to deal with file positions:
- tell()
- seek()
tell()
The tell() is used for identifying which position the current file pointer is located. In other words, it’s the distance of the file pointer from the beginning of the file.
Syntax:
file_object.tell()
Example
f=open("sample.txt","r") print(f.read()) # reads the file print(f.tell()) # the file is read thus the file pointer # is now at the end of the contents # in the file f.close()
Output
File handling in python… Happy learning…. 45
seek()
The seek() method is used to change the current file position. This function can be written with either 1 or 2 parameters.
Syntax:
file_object(offset) file_object (offset, from)
Explanation
offset – the offset value indicates the number of bytes or location of the file pointer to be moved. A positive number indicates right to left and a negative number indicates left to right.
from – the from parameter can be of only 3 values. If it is 0, then it indicates the pointer must be at the beginning of the file. If its 1, then the current position of the file pointer has to be considered. If it is 2, then it indicates the end of the file.
Example 1
f=open("sample.txt", "r") print(f.read()) f.seek(20) print(f.tell())
Output
File handling in python... Happy learning.... 20
Example 2
f=open("sample.txt", "r") f.seek(5,0) print(f.read())
Output
handling in python... Happy learning....
More file operations
The os module can be used for file operations and directory related operations in python. To perform those operations, the os module needs to be imported first.
- rename()– can be used to rename a file
- remove() – remove or delete a file
Syntax:
os.rename(old_file_name. new_file_name) os.remove(file_name)
Example 1:
import os os.rename( “sample.txt”, ”example.txt)
Output
The text file sample.txt is changed to example.txt
Example 2:
import os os.remove(“example.txt”)
Output
The example.txt file is deleted
Directory operations
There are 4 common directory operations which you can perform in python file handling:
- mkdir() – creates a new directory
- chdir() – changes the directory
- getcwd() – returns the current directory
- rmdir() – removes the directory
Example 1
import os os.mkdir(“practice”)
Output
Creates a new directory called practice
Example 2
import os os.chdir(“/desktop/sample”)
Output
Changes the location of the directory to the specified path
Example 3
import os os.getcwd()
Output
Returns the current working directory
Example 4
import os os.rmdir(“/sample”)
Output
Removes the specified directory
This post is contributed by Kushmitha Radhakrishnan.