What is Python File Handling?
Python File handling is the process, if you are working in a large software application where they process a large number of data, then we cannot expect those data to be stored in a variable as the variables are volatile in nature.
Hence when are you about to handle such situations, the role of files will come into the picture.
As files are non-volatile in nature, the data will be stored permanently in a secondary device like Hard Disk and using python we will handle these files in our applications.
Read(), Write(), Close() and Delete() File Handling Methods in Python
List of Topics
Python Open() file method
Python close() file method
Python write() file method
Python Read() file method
Python rename(), deleting file method
Open() file method In python
Before perform read and write operation on file, you need to open it first.
To perform open method, we will use its pre-built python open() method. It return a file object so that we can use it to modify the file.
Syntax for file open in python:
file object = open(file_name, file_mode, file_buffering)
file_name: It is a name of file which is string representing the name of file.
file_mode: It is representing as a integer. There are different types of file mode used to perform file operation
Here the list of different types of file_mode which used to perform action:
<r> It opens a file in read-only mode
<w> It opens the file in write mode
<a> It opens the file in append mode, used to add something to the file at offset end position
other file_mode <r+>, <rb>, <w+>, <ab>, <a+>, <ab>, <ab+>, etc.
f_open = open("demo.txt", "a")
By this you can add something to the file at the end position
Close() file method in python
In python we will use method close() to perform close operation on file it is predefined method of file
f = open("demo.txt", "a") f.close()
Auto Close Using 'with'
When we will open file with the help of 'with' then we will not use close() method in this.
with open('app.txt', 'a') as f:
Write() file method in python
To perform write() operation in file we used (read/write/append )
Python provides write() method to write a string or sequence of bytes to a file.
with open('app.text', 'w') as f: f.write('hi\n') f.write('This is a python code blog\n') with open('app.text', 'r') as f: content = f.readlines() for line in content: print(line)
hi This is a python code blog
read() file method in python
Before read data from a file it is necessary to open it in read(<r>) mode
we use python read(size) function to read the content of a file up to specified size
with open('app.txt', 'w') as f: f.write('hi\n') f.write('This is a python code blog\n') f= open('app.txt', 'r') print(f.read(10)) # read the first 10 data
How to set file offset in python
Find the current offset
Use tell() method to find the current offset
If you want to change the position of pointer the use seek() method
with open('app.txt', 'w') as f: f.write('hi\n') f.write('This is a python code blog\n') f= open('app.txt', 'r') print(f.read(10)) # read the first 10 data # check current position position = f.tell(); print('current file position :', position)
Rename() file method in python
To rename file in python use rename() file method
To do this first import the os module in python
import os os.rename('app.txt', 'app1.txt')
Remove() file method in python
Use remove() method to remove file from python file
import os os.remove('app.txt')
Some Important python file object methods:
Here we give some important python file object methods which important to perform any action on file in python
<file.close>, <file.readline()>, <file.write()>, <file.read(size)>, etc