1.File handling is a mechanism to store the data on the disk permanently.
2.There are several functions for creating ,reading,writing ,updating and deleting files.
Operation on File
1.Opening of file.
2.Writing into a file.
3.Appending data into a file.
4.Reading from a file.
5.Closing of file.
File Opening Modes
In Python File can be open in different mode to perform read and write operation on a file.open() function is used to open a file,open function takes two arguments.
open(filename,open mode); for example if you want to open easy.txt file in writing mode then you should write
fileptr = open("easy.txt","w")
Different file opening mode is given below
S.No. | Mode | Description |
1. | w | Open a file for writing Creates a new file if it does not exist or truncates the file if it exists. |
2. | a | Open a file for appending data at the end of file. It does not truncate the file. It creates new file if file does not exist. |
3. | r | Open a file for reading. |
4. | t | Opens a file in text mode. |
5. | b | Opens a file in binary mode. |
6. | w+ | Opens a file for both writing and reading. |
7. | r+ | Opens a file for both reading and writing. The file pointer placed at the beginning of the file. |
>Writing into a file
- write() function is used to write content to a file.
# open the easy.txt file in D drive in write mode
# Creates a new file if no such file exists.
fileptr = open("D:/easy.txt", "w");
# Overwriting the content to the file
fileptr.write("Hello friends how are you?")
# closing the opened file
fileptr.close();
***File Output***
Hello friends how are you?
"""
Appending data into a file
# open the easy.txt file in D drive in append mode
# Creates a new file if no such file exists.
fileptr = open("D:/easy.txt", "a");
# Appending the content to the file
fileptr.write("\nI am fine and how are you?")
# closing the opened file
fileptr.close();
"""
***File Output***
Hello friends how are you?
I am fine and how are you?
"""
Reading from a file
- read() function is used read content of a file.
- Syntax of read() function:read(count).
- Here count is number of bytes to be read from the file starting from the beginning of file.
- count is optional.
- If the count is not specified, then it may read the content of the file until the end.
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading the content of the file
#and storing into a variable
filedata=fileptr.read()
print("File data:",filedata)
# closing the opened file
fileptr.close();
"""
***File Output***
File data: Hello friends how are you?
I am fine and how are you?
"""
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading 10 bytes of the file
#and storing into a variable
filedata=fileptr.read(10)
print("10 bytes of the file is :",filedata)
# closing the opened file
fileptr.close();
"""
***File Output***
10 bytes of the file is : Hello frie
"""
- readline() function is used to read a file line by line from the beginning of the file.
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading lin1 of the file
line1=fileptr.readline()
print("Line1:",line1)
# closing the opened file
fileptr.close();
"""
***File Output***
Line1: Hello friends how are you?
"""
- readline() function is used to read a file line by line from the beginning of the file.
- If we want to read two lines of the file then just use readline() method two times.
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading lin1 of the file
line1=fileptr.readline()
line2=fileptr.readline()
print("Line1:",line1)
print("Line2:",line2)
# closing the opened file
fileptr.close();
"""
***File Output***
Line1: Hello friends how are you?
Line2: I am fine and how are you?
"""
#suppose the file content is
#myeasy456@gmail.com
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading content of the file
filecontent=fileptr.read()
#initializing the counter with 0
count=0
#loop through filecontent
for i in filecontent:
#incrementing the counter
count=count+1;
print("Total Characters:",count)
# closing the opened file
fileptr.close();
"""
***File Output***
Total Characters: 19
"""
#suppose the file content is
#myeasy456@gmail.com
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading content of the file
filecontent=fileptr.read()
#initializing the counter with 0
count=0
#loop through filecontent
for i in filecontent:
#condition for alphabet
if i.isalpha():
#incrementing the counter
count=count+1;
print("Total Alphabets:",count)
# closing the opened file
fileptr.close();
"""
***File Output***
Total Alphabets: 14
"""
#suppose the file content is
#myeasy456@gmail.com
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading content of the file
filecontent=fileptr.read()
#initializing the counter with 0
count=0
#loop through filecontent
for i in filecontent:
#condition for digit
if i.isdigit():
#incrementing the counter
count=count+1;
print("Total digits:",count)
# closing the opened file
fileptr.close();
"""
***File Output***
Total digits: 3
"""
#suppose the file content is
#myeasy456@gmail.com
# open the easy.txt file of D drive in read mode
fileptr = open("D:/easy.txt", "r");
# Reading content of the file
filecontent=fileptr.read()
#initializing the counter with 0
alphnumeric=0
special=0
#loop through filecontent
for i in filecontent:
#condition for digit
if i.isalnum():
#incrementing alphanumeric counter
alphnumeric=alphnumeric+1
else:
#incrementing special symbol counter
special=special+1;
print("Total Special Symbols:",special)
print("Total Aphanumerics:",alphnumeric)
# closing the opened file
fileptr.close();
"""
***File Output***
Total Special Symbols: 2
Total Aphanumerics: 17
"""
0 Comments