File Handle
File Handle
File Handling
# To open a text file in read mode .
fh=open(r'C:\Users\Lenevo\Document\myprofile.txt','r')
print(fh.read())
Output:
2. Class - XII
file1 = open("myfile.txt","w")
file1.write("Hello \n")
file1.writelines(L)
file1 = open("myfile.txt","r+")
print(file1.read())
print()
file1.seek(0)
print(file1.readline())
print()
file1.seek(0)
print(file1.read(9))
print()
file1.seek(0)
print(file1.readline(9))
file1.seek(0)
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
file1.writelines(L)
file1.close()
file1.write("Today \n")
file1.close()
file1 = open("myfile.txt","r")
print("Output of Readlines after appending")
print(file1.readlines())
print()
file1.close()
file1.write("Tomorrow \n")
file1.close()
file1 = open("myfile.txt","r")
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']
Specific position.
f = open("demofile.txt", "r")
f.seek(n)
print(f.readline())
data = f.read()
print(type(data))
print(data)
f.close()
if content in ‘string.bin’ is b’GeeksForGeeks’ then
Output:
count = 0
rec= “ “
while True :
rec = fh.readline()
if rec== “ “:
break
count = count + 1
fh.close()
read=fh.read(20)
fp=fh.tell()
# OUTPUT:
The first 20 bytes are: This is class 12 com
The file pointer is at 20 position.