0% found this document useful (0 votes)
62 views29 pages

File Handling - Part-2 (TXT Files)

The document discusses various Python methods for reading and writing files, including: - The write() and read() methods to write strings to a file and read the file contents. - The writelines() method to write multiple strings to a file by passing an iterable like a list or tuple. - The tell() and seek() methods to get the current file position and move to specific positions. - Opening files in different modes like 'r', 'w', 'a' for read, write and append and using close() to flush content to disk.

Uploaded by

Harshil Sachde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views29 pages

File Handling - Part-2 (TXT Files)

The document discusses various Python methods for reading and writing files, including: - The write() and read() methods to write strings to a file and read the file contents. - The writelines() method to write multiple strings to a file by passing an iterable like a list or tuple. - The tell() and seek() methods to get the current file position and move to specific positions. - Opening files in different modes like 'r', 'w', 'a' for read, write and append and using close() to flush content to disk.

Uploaded by

Harshil Sachde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

write() ,read() Method based program

f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.read()
print(text)
f.close()
OUTPUT Welcome to python.mykvs.in
Regularly visit python.mykvs.in
TRY IT
m=open("earth.txt","a")
s=56
m.write(s)
m.close()
m=open("earth.txt","r")
print(m.read())
m.close()
m=open("earth.txt","a")
s=56
m.write(s)
m.close()
m=open("earth.txt","r")
print(m.read())
m.close()
EXTRA DOSE
O If numeric data are to be written to a text file, the data
need to be converted into string before writing to the file.

>>>myobject=open("myfile.txt",'w')
>>> marks=58
#number 58 is converted to a string using #str()
>>> myobject.write(str(marks))
2
>>>myobject.close()

O The write() actually writes data onto a buffer. When the


close() method is executed, the contents from this buffer
are moved to the file located on the permanent storage.
The writelines() method
O This method is used to write multiple strings to a file.
We need to pass an iterable object like lists, tuple, etc.
containing strings to the writelines() method.
O write(), the writelines() method does not return the
number of characters written in the file.

myobject=open("myfile.txt",'w')
lines = ["Hello everyone\n", "Writing #multiline
strings\n", "This is the #third line"]
myobject.writelines(lines)
myobject.close()
Tuple
myobject=open("myfile.txt",'w')
lines = ("Hello everyone\n", "Writing #multiline
strings.\n This is the #third line")
myobject.writelines(lines)
myobject.close()
m=open("myfile.txt",'r')
print(m.read())
m.close()
WAP
To copy the content of one file
to another file
PROGRAM
O Write a program that accepts a string from the user and
writes it to a text file and display it on screen.
O (i) read only 10 characters
O (ii) read one line only
O (iii) read complete file
Consider poem.txt and do the
following questions
O Reading a file to read a file first 30 bytes and printing it.
O Reading n bytes and then reading some more bytes till
end of the file.
O Reading an entire file
O Reading a first-three lines-line by line
O Reading a complete file-line by line
O Displaying the size of file after removing EOF character,
leading and trailing white spaces and blank lines.
O Reading the complete file in a list
PROGRAMS
Q1: Write a program to display the size of file in bytes.
Q2: Write a program to display the number of lines in the
file.
flush()
Seek and tell methods
tell()
O The tell() of python tells us the current position with in
the file.
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit
python.mykvs.in'
f.write(line)
f.close()
f = open("a.txt", 'rb+')
print(f.tell())
print(f.read(7)) # read seven characters
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0) # moves to 9 position from begining
print(f.read(5))
f.seek(4, 1) # moves to 4 position from current
location
print(f.read(5))
f.seek(-5, 2) # Go to the 5th byte before the end
print(f.read(5))
f.close()
Modify a Text file
O Replace string in the same File
fin = open("poem.txt", "r")
data = fin.read()
data = data.replace('we', 'HUM')
fin.close()
fin = open("poem.txt", "w")
fin.write(data)

fin.close()
WORKING
What have we done here?
Open file poem.txt in read text mode r.
fin.read() reads whole text of poem.txt to the variable data.
data.replace() replaces all the occurrences of my with your in
the whole text.
fin.close() closes the input file poem.txt.
In the last three lines, we are opening poem.txt in write text w
mode and writing the data to poem.txt in replace mode. Finally
closing the file poem.txt
Note :-above program is suitable for file with small size.

You might also like