0% found this document useful (0 votes)
13 views9 pages

Unit III File Handling, Classes - Part3

The document provides examples of file operations in Python, including reading, writing, and appending to files. It explains different access modes such as 'r' for reading, 'w' for writing, and the use of functions like write() and writelines(). Additionally, it covers how to delete files using the os library after closing the file.

Uploaded by

Saraswathi
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)
13 views9 pages

Unit III File Handling, Classes - Part3

The document provides examples of file operations in Python, including reading, writing, and appending to files. It explains different access modes such as 'r' for reading, 'w' for writing, and the use of functions like write() and writelines(). Additionally, it covers how to delete files using the os library after closing the file.

Uploaded by

Saraswathi
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

example

Counts number of lines and print them.


f=open("D:/test.txt","r")
for x in f:
print (x)
_______
output:
hello everyone, welcome to python programming classes
this is a appending operation in file
access modes:read
 r-read: only reading
 file must exsits before opening file
 file pointer points at the begining of the file.
write mode
 w-writing contents into the file
 file exists-begining of file and over writtens the data
 not exists-new files is created
append()
 file exists :pointer points at the end of the content.
 new file is created
writing to the file
 write()- single line
 writelines()-multi line
example:
#reading file and display the contents
f=open("D:/test.txt","r")
print(f.read())
_____________
output:
hello everyone, welcome to python programming classes
example:
f=open("D:/test.txt","w")
print(f.write("this is a appending operation in file"))
--------------
output:
37
contin
f=open("D:/test.txt","r")
for x in f:
print (x)
--------------------
output:
this is a appending operation in file
delete()
 first close the opened file.
 f.close()
 import library os
 syntax:
 os.remove(“path of file”)

You might also like