File Handling in Python programming | Hands on Coding

1.File handling may be a mechanism to store the info on the disk for good.
2.There area unit many functions for making ,reading, writing ,updating and deleting files.

Operation on File

1.Opening of file.
2.Writing into a file.
3.Appending knowledge into a file.
4.Reading from a file.
5.Closing of file.

File gap Modes

In Python File is open in numerous mode to perform scan and write operation on a file.open() perform is employed to open a file,open perform takes 2 arguments.
open(filename,open mode); as an example if you would like to open straightforward.txt come in writing mode then you must write
fileptr = open("easy.txt","w")
Different file gap mode is given below

S.No.ModeDescription
1.wOpen a file for writing
Creates a new file if it does not exist or truncates the file if it exists.
2.aOpen 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.rOpen a file for reading.
4.tOpens a file in text mode.
5.bOpens 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

  • Reading all the contents of the file
  • read() perform is employed scan content of a file.
  • Syntax of read() function:read(count).
  • Here count is variety of bytes to be scan from the file ranging from the start of file.
  • count is optional .
  • If the count isn't nominal, then it's going to scan the content of the file till the tip.

# 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() perform is employed to scan a file line by line from the start of the file.
  • If we would like to scan 2 lines of the file then simply use readline() methodology twice.

# 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
"""