Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR

TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)

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.

Write this code by opening a file new in Python:


dfh=open("demo.txt",'r')
data=dfh.read()
print(data)
dfh.close()

Reading from a File:


1. Read()
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
2. Read(n)
3. Readline()
4. Readlines()
Practical: 2
f=open("file.txt",'r')
print(f.read(2))
#print(f.readlines())
print(f.read(3))
#print("Rest of Data")
#print(f.read())

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()

Note: In the program, data present in the file shall be


overridden (or overwritten) every time we run this program
In order to avoid this situation we should add new text to
already-existing text using “a” append mode.

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()

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08)


[MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>>
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Pyth
on38-32/addingnumerictofile.py
>>>

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

Q. To add list items to a file using writelines() method


DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
f=open("file.txt",'w')
list=["cs\n","IP\n","phy\n","che\n"]
f.writelines(list)
f=open("file.txt",'r')
f1=f.read()
print(f1)
print("Written successfully")
f.close()
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Pyth
on38-32/writelist to file.py
cs
IP
phy
che

Written successfully

#using with statement to create file


with open("file.txt",'w') as f:
f.write("My file comtents\n")
f.write("My file comtents-Python\n")
#with read("file.txt",'r') as f:
#f1=f.read()
#print(f1)
print("is file closed",f.closed)
print("Is file closed:",f.close())

= 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

Q: Program to accept value from user and store it in a text


file.

name=input("Enter your name")


age=input("enter your age")
f=open("file.txt",'a')
f.write(name)
f.write(age)
f=open("file.txt",'r')
f1=f.read()
print(f1)
f.close()
output:
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Pyth
on38-32/inputfromuser.py
Enter your nameops
enter your age42
My file comtents
My file comtents-Python
you are welcome
Hope file has contents
ops42

Binary File Operation:

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'}

To check path of python:

#To check the path of Python


import os
cwd=os.getcwd()
print(cwd)
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Py
thon38-32/tocheckpath.py
C:\Users\DPSVNATL\AppData\Local\Programs\Python\Py
thon38-32

Standard file stream:

#Program to implement standard streams


import sys
f1=open(r"file2.txt")
line1=f1.readline()
line2=f1.readline()
DELHI PUBLIC SCHOOL NTPC VIDYUT NAGAR
TOPIC: DATA FILE HANDLING (PRACTICAL NOTES)
line3=f1.readline()
sys.stdout.write(line1)
sys.stdout.write(line2)
sys.stdout.write(line3)
= RESTART:
C:/Users/DPSVNATL/AppData/Local/Programs/Python/Pyth
on38-32/standaredfilestream.py
Delhi Public School
ntpc vidyut nagar
g b nagar

Random Access in files using TELL() and SEEK()

#illustrating seek() and tell()

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

Reading/Writing from CSV file


import csv
f=open("student.csv",'r')
csv_reader=csv.reader(f)
#reader() function read the file object
#reading student file record by record
for row in csv_reader:
print(row)
f.close()

= 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)

You might also like