0% found this document useful (0 votes)
10 views

File Handling Complete Notes With Programs

Uploaded by

hod.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

File Handling Complete Notes With Programs

Uploaded by

hod.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

File handling

A file is a sequence of bytes on the disk/permanent storage where a group of related


data is stored. File is created for permanent storage of data.InprogrammingFile
handling in Python enables us to create, update, read, and deletethe files stored on the
file system through our python program. The following operations can be performed
on a file.
In Python, File Handling consists of following three steps:

Open the file.


Process file i.e perform read or write operation.
Close the file.
Types of File

There are two types of files:

Text Files- A file whose contents can be viewed using a text editor is called a
text file. A text file is simply a sequence of ASCII or Unicode characters. Python
programs, contents written in text editors are some of the example of text
files.

Binary Files-A binary file stores the data in the same way as as stored in the
memory. The .exe files,mp3 file, image files, word documents are some of the
examples of binary files.we can’t read a binary file using a text editor.

Text File Binary File


Its Bits represent Its Bits represent a
character. custom data.
Less prone to get
corrupt as change Can easily get
reflects as soon as corrupted, corrupt on
made and can be even single bit change
undone.
Can store different
Store only plain text in types of data
a file. (audio, text,image) in a
single file.
Developed for an
Widely used file
application and
format and can be
can be opened in that
opened in any text
application
editor.
only.
Mostly .txt and .rtf are Can have any

pg. 1
used as application defined
extensions to text files. extension
Opening and Closing FilesTo perform file operation ,it must be opened first
then after reading ,writing, editing operation can be performed. To create any
new file then too it must be opened. On opening of any file ,a file relevant
structure is created in memory as well as memory space is created to store
contents. Once we are done working with the file, we should close the file.
Closing a file releases valuable system resources. In case we forgot to close
the file, Python automatically close the file when program ends or file object is
no longer referenced in the program. However, if our program is large and we
are reading or writing multiple files that can take significant amount of
resource on the system. If we keep opening new files carelessly, we could run
out of resources. So be a good programmer , close the file as soon as all task
are done with it.
open Function :Before any reading or writing operation of any file,it must be
opened firstof all. Python provide built in function open() for it. On calling of
this function creates file object for file operations.
Syntax
file object = open(<file_name>, <access_mode>,< buffering>)
file_name = name of the file ,enclosed in double quotes.
access_mode= Determines the what kind of operations can be performed with
file,likeread,write etc.
Buffering = for no buffering set it to 0.for line buffering set it to 1.if it is
greater than 1 ,then it is buffer size.if it is negative then buffer size is system
default.
File opening modes
Sr.
No Mode & Description
.
r - readingonly.Sets file
pointer at beginning of
1
the file . This is the
default mode.
rb– same as r mode
2
but with binary file
r+ - both reading and
writing. The file
3
pointer placed at the
beginning of the file.
rb+ - same as r+ mode
4
but with binary file
w - writing only.
5
Overwrites the file if

pg. 2
the file exists. If not,
creates a new file for
writing.
wb– same as w mode
6
but with binary file.
w+ - both writing and
reading. Overwrites . If
7
no file exist, creates a
new file for R & W.
wb+ - same as w+
8 mode but with binary
file.
a -for appending. Move
file pointer at end of
9 the file.Creates new
file for writing,if not
exist.
ab– same as a but with
10
binary file.
a+ - for both
appending and
reading. Move file
pointer at end. If the
11
file does not exist, it
creates
a new file for reading
and writing.
ab+ - same as a+ mode
12
but with binary

File object attributes –


closed: It returns true if the file is closed and false when the
file is open.
encoding: Encoding used for byte string conversion.
mode: Returns file opening mode
name: Returns the name of the file which file object holds.
newlines: Returns “\r”, “\n”, “\r\n”, None or a tuple
containing all the newline types seen.

E.g. Program
f = open("a.txt", 'a+')
print(f.closed)
print(f.encoding)

pg. 3
print(f.mode)
print(f.newlines)
print(f.name)

OUTPUT
False
cp1252
a+
None
a.txt
The close() Method
close(): Used to close an open file. After using this method,an
opened file will be closed and a closed file cannot be read or written any
more.

E.g. program
f = open("a.txt", 'a+')
print(f.closed)
print("Name of the file is",f.name)
f.close()
print(f.closed)

OUTPUT
False
Name of the file is a.txt
True
The write() Method
It writes the contents to the file in the form of string. It does not return value.
Due to buffering,the string may not actually show up in the file until the
flush() or close() method is called.

The read() Method


It reads the entire file and returns it contents in the form of a string. Reads at
most size bytes or less if end of file occurs.if size not mentioned then read the
entire file contents.
write() ,read() Method based program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')

pg. 4
text = f.read()
print(text)
f.close()

OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
readline([size]) method: Read no of characters from file if size is mentioned
till eof.read line till new line character.returns empty string on EOF.

e.g. program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.readline()
print(text)
text = f.readline()
print(text)
f.close()

OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
readlines([size]) method: Read no of lines from file if size is mentioned or all
contents if size is not mentioned.

e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.readlines(1)
print(text)
f.close()

pg. 5
OUTPUT
['Welcome to python.mykvs.in\n']
NOTE – READ ONLY ONE LINE IN ABOVE PROGRAM.
Iterating over lines in a file

e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
for text in f.readlines():
print(text)
f.close()
Processing Every Word in a File

e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
for text in f.readlines():
for word in text.split( ):
print(word)
f.close()

OUTPUT
Welcome
to
python.mykvs.in
Regularly
visit
python.mykvs.in
Append content to a File
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit python.mykvs.in'
f.write(line)
f.close()

pg. 6
f = open("a.txt", 'a+')
f.write("\nthanks")
f.close()
f = open("a.txt", 'r')
text = f.read()
print(text)
f.close()

OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
thanks
Getting & Resetting the Files Position

The tell() method of python tells us the current position within the file,where
as the seek(offset[, from]) method changes the current file position. If from is 0,
the beginning of the file to seek. If it is set to 1, the current position is used . If
it is set to 2,then the end of the file would be taken as seek position. The offset
argument indicates the number of bytes to be moved.

e.g.program
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit python.mykvs.in'
f.write(line)
f.close()
f = open("a.txt", 'rb+')
print(f.tell())
print(f.read(7)) # read seven characters
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0) # moves to 9 position from begining
print(f.read(5))
f.seek(4, 1) # moves to 4 position from current location
print(f.read(5))
f.seek(-5, 2) # Go to the 5th byte before the end
print(f.read(5))
f.close()

OUTPUT
0
b'Welcome'
7
b' to

pg. 7
python.mykvs.in\r\n
Regularly visit
python.mykvs.in'
59
b'opyt'
b'mykvs'
b'vs.in'

Methods of os module

1. The rename() method used to rename the file.


syntax
os.rename(current_file_name, new_file_name)
2. The remove() method to delete file.
syntax
os.remove(file_name)
3.The mkdir() method of the osmodule to create
directories in the current directory.
syntax
os.mkdir("newdir")
4.The chdir() method to change the current directory.
syntax
os.chdir("newdir")
5.The getcwd() method displays the current directory.
syntax
os.getcwd()
6. The rmdir() method deletes the directory.
syntax
os.rmdir('dirname')

e.g.program
import os
print(os.getcwd())
os.mkdir("newdir")
os.chdir("newdir")
print(os.getcwd())
File Handling

Absolute Path vs Relative Path


The absolute path is the full path to some place on yourcomputer. The relative
path is the path to some file with respectto your current working directory
(PWD). For example:
Absolute path: C:/users/admin/docs/staff.txt

pg. 8
If PWD is C:/users/admin/, then the relative path to staff.txt wouldbe:
docs/staff.txt
Note, PWD + relative path = absolute path.

os.chdir("C:/users/admin/docs")
os.path.exists("staff.txt")
This returns TRUE if stuff.txt exists and it works.
Now, instead if we write,
os.path.exists("C:/users/admin/docs/staff.txt")
This will returns TRUE.
If we don't know where the user executing the script from, it is best to
compute the absolute path on the user's system using os and __file__.
__file__ is a global variable set on every Python script that returns the relative
path to the *.py filethat contains it.

e.g.program
import os
print(os.getcwd())
os.mkdir("newdir1")
os.chdir("newdir1")
print(os.getcwd())
my_absolute_dirpath =
os.path.abspath(os.path.dirname(__file__))
print(my_absolute_dirpath)
Copying the contents of one text file to another
a=open("tocopy.txt","r")
x=a.read()
b=open("copy.txt","w")
b.write(x)
print("copied")
a.close()
b.close()

Copying alternate lines in 2 files


with open('file1.txt') as fh1, open('file2.txt') as fh2:
for line1, line2 in zip(fh1, fh2):

Use of writelines function


f = open('test2.txt','w')
str = "hello world.\n this is my first file handling program.\n I am using
python language"
f.writelines(str)
f.close()

pg. 9
Entering data segregated with colon
count=int(input("Enter the number of students"))
f=open("separator1","a")
fori in range(count):
Roll=int(input("Enter the rollno"))
Name=input("Enter the name of the student")
Mark=int(input("Enter the mark"))
rec=str(Roll)+":"+Name+":"+str(Mark)+"\n"
f.write(rec)
print(rec)
f.close()
f=open("separator1","r")
for line in f:
cols=line.split(":")
x=cols[0]
y=cols[1]
z=cols[2]
print(x,y,z)
f.close()

Write a program to create a text file and do the following

1. Finding the number of words


2. Finding the upper case alphabets

Finding the number of words


file = open("C:\data.txt", "r”)
data = file.read()
words = data.split()
print('Number of words in text file :', len(words))

Finding the upper case alphabets


string=input("Enter string:")
count1=0
count2=0
foriinstring:
if(i.islower()):
count1=count1+1
elif(i.isupper()):

pg. 10
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)

Write a program to create a text file and do the following


1. To count the occurrence of a particular word
2. To count the vowels

To count the occurrence of a particular word

file = open("data.txt", "r")


#read content of file to string
data = file.read()
#get number of occurrences of the substring in the string
occurrences = data.count("python")
print('Number of occurrences of the word :', occurrences)
To count the vowels

open("d:/mytext.txt","r")

str1=file1.read()

vowel_count=0
foriinstr1:
if(i=='A'ori=='a'ori=='E'ori=='e'ori=='I'
ori=='i'ori=='O'ori=='o'
ori=='U'ori=='u'):
vowel_count+=1

print('The Number of Vowels in text file :',vowel_count)

file1.close()

Write a program to create a text file and do the following


1. To count the number of characters

pg. 11
2. Finding the lines starting with a particular character ‘A’.

To count the number of characters


file = open("C:\data.txt", "r")
#read the content of file
data = file.read()
#get the length of the data
number_of_characters = len(data)

print('Number of characters in text file :', number_of_characters)


Finding the lines starting with a particular character ‘A’.
deffunc(l): # function to count lines starting with ‘A’
c = 0 # c is the count of such lines
for line in l:
if line[0] == ‘A’
c += 1
return c

f1 = open(‘LINES.txt’, ‘r’)
l = f1.readlines() # creates a list of all the lines in the file
print(‘Number of lines starting with A in the file LINES.txt is’, func(l))

Write a program to create a text file and do the following


1. To print the lines starting with a particular word
2. To count the number of lines in the file
1. f = open('testconn.txt')
m=f.readlines()
line=”Python”
for record in m:
s=record.split()
if line==s[0]:
print(record)

2. f = open('testconn.txt')
m=f.readlines()
print(len(m))

Write a program to create a text file and do the following

pg. 12
1. Copy only the lines with character ‘”B” to another file.
2. A text file “sample1” which has multiple spaces between each
word.
The contents of “sample1” to be copied to another file “sample2” where the
multiple spaces must be reduced to single space.
1. Copy only the lines with character ‘”B” to another file.
f=open(“mm.txt”,”r”)
f1=open(“mm1.txt”,”w”)
X=f.readlines()
for m in X:
if m[0][0]==”B”:
f1.writeline(m)
f.close()
f1.close()
f1=open(“mm1.txt”,”r”)
d=f1.read()
print(d)

A text file “sample1” which has multiple spaces between each word.
f=open(“sample.txt”,”r”)
f1=open(“sample1.txt”,”w”)
x=f.readlines()
fori in x:
z=i.split()
for j in z:
z1=j+’ ‘
f1.write(z1)
f.close()
f1.close()

Copying those lines which is not starting with ‘a’


x=open("ss.txt","r")
y=open("container2.txt","w")
s=x.readlines()
x1="a"
fori in s:
d=i.find(x1)
if d==-1:
y.write(i)
x.close()
y.close()

Binary file example

pg. 13
import pickle
defcreateFile():
ch="y"
fobj=open("EMP.dat","ab")
whilech=="y":
Empno=int(input("Empno : "))
Name=input("Name :")
Dept=input("Dept")
rec=[Empno,Name,Dept]
pickle.dump(rec,fobj)
x=input("Do you want to continue")
if x=="y":
continue
else:
break
fobj.close()

defCountRec(Dept):
fobj=open("EMP.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
ifDept==rec[2]:
num = num + 1
exceptEOFError:
fobj.close()
returnnum

#main
createFile()
City=input("Enter the Department")
X=CountRec(City)
print(X)

Create a binary file with the following details


Rollno,Name,Mark
1. Write a function create() to get the input.
2. Display the details of the students whose mark is >80

import pickle
defcreateFile():
ch=”y”

pg. 14
fobj=open("STU.dat","ab")
while ch=”y”:
EmpNo=int(input("Rollno : "))
Pro_name=input("Name :")
Dept=input(“Mark”)
rec=[Rollno,Name,Mark]
pickle.dump(rec,fobj)
x=input(“Do you want to continue”)
if x==”y”:
continue
else:
break
fobj.close()

defDispRec():
fobj=open("STU.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]>80
print(rec[0],rec[1],rec[2])
except:
fobj.close()

#main
Createfile()
Disprec()

'''Write a Python program to read a given CSV file having tab delimiter.
'''
import csv
with open('countries.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter = '\t')
for row in data:
print(', '.join(row))

'''Write a Python program that reads each row of a given csv file
and skip the header of the file.
Also print the number of rows and the field names. '''
import csv
fields = []
rows = []

pg. 15
with open('departments4.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ', quotechar=',')
# Following command skips the first row of the CSV file.
fields = next(data)
for row in data:
print(', '.join(row))
print("\nTotal no. of rows: %d"%(data.line_num))
print('Field names are:')
print(', '.join(field for field in fields))

#Writerow
import csv
with open('employee_file.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(['John Smith', 'Accounting', 'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

#way to write to csv file


import csv
file=open('Data2.csv','w')
writer = csv.writer(file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Programming language', 'Designed by', 'Appeared',
'Extension'])
writer.writerow(['Python', 'Guido van Rossum', '1991', '.py'])
writer.writerow(['Java', 'James Gosling', '1995', '.java'])
writer.writerow(['C++', 'Bjarne Stroustrup', '1985', '.cpp'])
file.close()
#If quoting is set to csv.QUOTE_MINIMAL, then .writerow() will quote
the fields only .
#if they contain the delimiter or the quotechar, then it will be default.
#If quoting is set to csv.QUOTE_ALL, then .writerow() will quote allfields.
#If quoting is set to csv.QUOTE_NONNUMERIC, then .writerow() will quote all
fields containing text data and convert all numeric fields to the float data type.
#If quoting is set to csv.QUOTE_NONE, then .writerow() will escape delimiters
instead of quoting them. In this case, you also must provide a value for the
escapechar. It is an optional parameter.

pg. 16
'''Write a Python program to write a Python dictionary
to a csv file. After writing the CSV file read the CSV
file and display the content. '''
import csv
csv_columns = ['id','Column1', 'Column2', 'Column3', 'Column4', 'Column5']
dict_data = {'id':['1', '2', '3'],
'Column1':[33, 25, 56],
'Column2':[35, 30, 30],
'Column3':[21, 40, 55],
'Column4':[71, 25, 55],
'Column5':[10, 10, 40], }
csv_file = "temp.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()#writeheader() method writes the headers to the CSV
file.
for data in dict_data:
writer.writerow(dict_data)
exceptIOError:
print("I/O error")
data = csv.DictReader(open(csv_file))
print("CSV file as a dictionary:\n")
for row in data:
print(row)'''Write a Python program to write a Python list of lists to a csv
file. After writing the CSV file read the CSV
file and display the content.'''
import csv
data = [[10,'a1', 1], [12,'a2', 3], [14, 'a3', 5], [16, 'a4', 7], [18, 'a5', 9]]
with open("temp.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
with open('temp.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
Create a CSV and do the following
1. Reading and writing
2. Reading the contents in the form of dictionary and display
3. Reading the contents in the form of list and print

1.READ

pg. 17
#import necessary modules
#csv_reader = csv.reader(csv_file, delimiter=',')
import csv
f=open("data.csv","r")
data = csv.reader(f)
for row in data:
print(row)

2.WRITE
#import necessary modules
import csv

file=open('Data2.csv','w')
writer = csv.writer(file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)

#way to write to csv file


writer.writerow(['Programming language', 'Designed by', 'Appeared',
'Extension'])
writer.writerow(['Python', 'Guido van Rossum', '1991', '.py'])
writer.writerow(['Java', 'James Gosling', '1995', '.java'])
writer.writerow(['C++', 'Bjarne Stroustrup', '1985', '.cpp'])
file.close()

3.import csv
data = [[10,'a1', 1], [12,'a2', 3], [14, 'a3', 5], [16, 'a4', 7], [18, 'a5', 9]]
with open("temp.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
with open('temp.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
4. '''Write a Python program to write a Python dictionary
to a csv file. After writing the CSV file read the CSV
file and display the content. '''
import csv
csv_columns = ['id','Column1', 'Column2', 'Column3', 'Column4', 'Column5']
dict_data = {'id':['1', '2', '3'],
'Column1':[33, 25, 56],
'Column2':[35, 30, 30],
'Column3':[21, 40, 55],

pg. 18
'Column4':[71, 25, 55],
'Column5':[10, 10, 40], }
csv_file = "temp.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()#writeheader() method writes the headers to the
CSV file.
for data in dict_data:
writer.writerow(dict_data)
exceptIOError:
print("I/O error")
data = csv.DictReader(open(csv_file))
print("CSV file as a dictionary:\n")
for row in data:
print(row)

pg. 19

You might also like