The document discusses various methods for reading and writing files in Python. It includes 11 programs that demonstrate: 1) Reading an entire file using read(), 2) Reading a specified number of bytes, 3) Reading the first line, 4) Reading into a list, 5) Reading line by line, 6) Getting number of lines, 7) Appending to a file and reading, 8) Writing to a binary file, 9) Reading a binary file, 10) Checking file pointer position, 11) Reading while skipping first bytes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
60 views7 pages
File Handling Practical
The document discusses various methods for reading and writing files in Python. It includes 11 programs that demonstrate: 1) Reading an entire file using read(), 2) Reading a specified number of bytes, 3) Reading the first line, 4) Reading into a list, 5) Reading line by line, 6) Getting number of lines, 7) Appending to a file and reading, 8) Writing to a binary file, 9) Reading a binary file, 10) Checking file pointer position, 11) Reading while skipping first bytes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7
Chapter = 3 = File Handling
# PROGRAM 1-'To read a file using read() method:'
fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') print(fh.read()) # OUTPUT: This is class 12 computer class.
I am Om kumar of class 12th A-1.
We study @@@@@@@@ Tulsi Vidya Niketan........ and also
My class teacher's name is Atma Ram Dubey sir....
Thank you. # PROGRAM 2-'Reading 'n' bytes from read() method:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') print(fh.read(51)) # OUTPUT: This is class 12 computer class. I am Om kumar # PROGRAM 3-'Reading the first line of the file from # readline() method :' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') print(fh.readline()) # OUTPUT: This is class 12 computer class. # PROGRAM 4-'Reading the file in the form of list:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') print(fh.readlines()) # OUTPUT: ['This is class 12 computer class. \n', ' \n', 'I am Om kumar of class 12th A-1.\n', 'We study @@@@@@@@ Tulsi Vidya Niketan........\n', ' and also\n', '\n', "My class teacher's name is Atma Ram Dubey sir....\n", 'Thank you.'] # PROGRAM 5-'Reading the entire file line by line:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') for line in fh: print ('The line is:',line,end='',) fh.close() # OUTPUT: The line is: This is class 12 computer class. The line is: The line is: I am Om kumar of class 12th A-1. The line is: We study @@@@@@@@ Tulsi Vidya Niketan........ The line is: and also The line is: The line is: My class teacher's name is Atma Ram Dubey sir.... The line is: Thank you. # PROGRAM 6-'To display the number of lines:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') lines=fh.readlines() size=len(lines) print('Number of lines in file is:',size,'lines') # OUTPUT: Number of lines in file is: 8 lines # PROGRAM 7-'To write furthur into a file and read: ' fh=open(r'C:\Users\narendra\Documents\computer12.txt','a+') str1='My class mates are Ayush sand Sarvesh.' fh.write(str1) print(fh.read()) # OUTPUT: This is class 12 computer class.
I am Om kumar of class 12th A-1.
We study @@@@@@@@ Tulsi Vidya Niketan........ and also
My class teacher's name is Atma Ram Dubey sir....
Thank you.My class mates are Ayush sand Sarvesh. # PROGRAM 8-'To write into a binary file: ' import pickle stu={} stufile=open('stu.dat','ab') ans='y' while ans=='y': name=input('Enter name:') rno=int(input('Enter the roll no:')) marks=float(input('Enter marks:')) stu['Rollno']=rno stu['Name']=name stu['Marks']=marks pickle.dump(stu,stufile) ans=input('Want to append more records?(y/n)...') stufile.close() # OUTPUT: Enter name:Om Enter the roll no:24 Enter marks:60 Want to append more records?(y/n)...y Enter name:Sarvesh Enter the roll no:32 Enter marks:64 Want to append more records?(y/n)...n # PROGRAM 9-'To read a binary file': import pickle stufile=open('stu.dat','rb') nemp=pickle.load(stufile) print(nemp) # OUTPUT: {'Rollno': 24, 'Name': 'Om', 'Marks': 60.0} # PROGRAM 10-'To check the file pointer position:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') read=fh.read(20) print('The first 20 bytes are:',read) fp=fh.tell() print('The file pointer is at',fp,'position.') # OUTPUT: The first 20 bytes are: This is class 12 com The file pointer is at 20 position. # PROGRAM 11-'To read the file leaving first 50 bytes:' fh=open(r'C:\Users\narendra\Documents\computer12.txt','r') #placing the file pointer after 50 bytes fh.seek(50) #reading the data after 50 bytes print(fh.read()) # OUTPUT: mar of class 12th A-1. We study @@@@@@@@ Tulsi Vidya Niketan........ and also