Practical Notes DataFileHandling
Practical Notes DataFileHandling
Practical: 1
Create a file with text given below in Notepad software to
create .txt File:
Welcome in Python Programming Language.
We are Discussing Data File Handling in Python.
I Hope You understand this Chapter.
Do practical as discussed in class.
He
Llo
He
['llo\n', 'welcome\n', 'python\n', 'files']
Rest of Data
>>>
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Python38
-32/Dfhtwocharacter.py
He
['llo\n', 'welcome\n', 'python\n', 'files']
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
>>>
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Python38
-32/Dfhtwocharacter.py
He
Writing to file:
1. Write(string)
2. Writelines(sequence of lines)
f=open("file.txt",'w')
#print(f.read(2))
#print(f.readlines())
#print(f.read(3))
#print("Rest of Data")
#print(f.read())
f.write("Hope u understand \n")
#print("Data written to the file successfully")
f=open("file.txt",'r')
a=f.read()
print(a)
f.close()
f=open("file.txt",'w')
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
x=100
f.write("Hello\n")
f.write(str(x))
f.close()
f=open("file.txt",'w')
x=100
f.write("Hello\n")
f.write(str(x))
f=open("file.txt",'r')
f2=f.read()
print(f2)
f.close()
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Pyth
on38-32/addingnumerictofile.py
Hello
100
Written successfully
= RESTART:
C:\Users\DPSVNATL\AppData\Local\Programs\Python\Pyth
on38-32\usewithstat.py
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
is file closed True
Is file closed: None
import pickle
dict1={'python:90','vb:80','c:85'}
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
f=open('file1.dat','wb')
pickle.dump(dict1,f)
f=open('file1.dat','rb')
dict1=pickle.load(f)
print(dict1)
f.close()
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/binaryfile1.py
{'vb:80', 'c:85', 'python:90'}
f=open("file2.txt")
print("Before reading",f.tell())
s=f.read()
print("After reading",f.tell())
f.seek(0)
#Bringing file pointer to the 0th byte so data can be
#written/read from the next byte
print("From the beginning again", f.tell())
s=f.read(4)
print("First four bytes are:",s)
print(f.tell())
s=f.read(3)
print("Next 3 bytes:",s)
print(f.tell())
f.close()
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/tellseek.py
Before reading 0
After reading 0
From the beginning again 0
First four bytes are:
0
Next 3 bytes:
0
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/csvfile.py
['NAME', 'CLASS', 'MARKS']
['AANYA', 'IX', '50']
['ANANYA', 'IX', '50']
['ANKITA', 'IX', '45']
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
import csv
fields=['name','class','marks']
rows=[['Mita','IX','78'],[['GEETA','IX','97']]]
fname='student.csv'
with open(fname,'a',newline='') as f:
csv_w=csv.writer(f,delimiter='.')
csv_w.writerow(fields)
for i in rows:
csv_w.writerow(i)
print("file created")
f=open("student.csv",'r')
csv_reader=csv.reader(f)
for rows in csv_reader:
print(rows)
f.close()
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/writeanddisplay,py.py
file created
['name.class.marks']
['Sita.IX.78']
["['REETA'", " 'IX'", " '97']"]
>>>
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/writeanddisplay,py.py
file created
['name.class.marks']
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
['Sita.IX.78']
["['REETA'", " 'IX'", " '97']"]
['name.class.marks']
['Mita.IX.78']
["['GEETA'", " 'IX'", " '97']"]
import csv
fields=['name','class','marks']
rows=[['Mita','IX','78'],[['GEETA','IX','97']]]
fname='student.csv'
with open(fname,'w',newline='') as f:
csv_w=csv.writer(f,delimiter='.')
csv_w.writerow(fields)
for i in rows:
csv_w.writerow(i)
print("file created")
f=open("student.csv",'r')
csv_reader=csv.reader(f)
for rows in csv_reader:
print(rows)
f.close()
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/writeanddisplay,py.py
file created
['name.class.marks']
['Mita.IX.78']
["['GEETA'", " 'IX'", " '97']"]
BY: O.P.Singh PGT(Dept. of CS)