Chapter -3
File Handling
# To open a text file in read mode .
fh=open(r'C:\Users\Lenevo\Document\myprofile.txt','r')
print(fh.read())
Output:
1. Name- SARVESH PANDEY
2. Class - XII
3. School- Tulsi Vidya Niketan
# Program to show various way to read and write data in a file.
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]
file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes
file1 = open("myfile.txt","r+")
print("Output of Read function is ")
print(file1.read())
print()
file1.seek(0)
print( "Output of Readline function is ")
print(file1.readline())
print()
file1.seek(0)
print("Output of Read(9) function is ")
print(file1.read(9))
print()
file1.seek(0)
print("Output of Readline(9) function is ")
print(file1.readline(9))
file1.seek(0)
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Output of Read(9) function is
Hello
The
Output of Readline(9) function is
Hello
Output of Readlines function is
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is
London \n']
#.Writing a program to illustrate append vs write mode.
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]
file1.writelines(L)
file1.close()
file1 = open("myfile.txt","a")#append mode
file1.write("Today \n")
file1.close()
file1 = open("myfile.txt","r")
print("Output of Readlines after appending")
print(file1.readlines())
print()
file1.close()
file1 = open("myfile.txt","w")#write mode
file1.write("Tomorrow \n")
file1.close()
file1 = open("myfile.txt","r")
print("Output of Readlines after writing")
print(file1.readlines())
print()
file1.close()
Output:
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London
\n', 'Today \n']
Output of Readlines after writing
['Tomorrow \n']
#.Writing a python program for change the position of the File Handle
to a given
Specific position.
f = open("demofile.txt", "r")
f.seek(n)
print(f.readline())
#.Writing a python program to return the current file position in a file
stream.
f = open("demofile.txt", "r")
print(f.readline())
print(f.tell())
#.Writing a program for opening a binary file in read
mode.
f = open("files.zip", mode="rb")
data = f.read()
print(type(data))
print(data)
f.close()
if content in ‘string.bin’ is b’GeeksForGeeks’ then
Output:
#.Writing a program to display all the records in a file along with
With/record number.
fh= open(“Result.det”,”r”)
count = 0
rec= “ “
while True :
rec = fh.readline()
if rec== “ “:
break
count = count + 1
print(count, rec, end=’ ‘)
fh.close()
# 'To check the file pointer position:'
fh=open(r'C:\Users\pc\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.