0% found this document useful (0 votes)
19 views6 pages

File Handle

This document discusses various methods of reading and writing files in Python, including: 1. Opening files in read, write, append, and binary modes. Methods like read(), readline(), readlines() are used to read file contents while write() writes to files. 2. The seek() method changes the file pointer position and tell() returns the current file position. 3. A program demonstrates counting lines and displaying record numbers when reading a file. 4. The differences between write and append modes are shown using an example.

Uploaded by

sarvesh2016.sp
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
19 views6 pages

File Handle

This document discusses various methods of reading and writing files in Python, including: 1. Opening files in read, write, append, and binary modes. Methods like read(), readline(), readlines() are used to read file contents while write() writes to files. 2. The seek() method changes the file pointer position and tell() returns the current file position. 3. A program demonstrates counting lines and displaying record numbers when reading a file. 4. The differences between write and append modes are shown using an example.

Uploaded by

sarvesh2016.sp
Copyright
© © All Rights Reserved
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/ 6

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.

You might also like