Professional Documents
Culture Documents
File Handling - 2
File Handling - 2
File Handling - 2
Closing Files :-
An open file is closed by calling function close() in Python otherwise error of data
loss is possible. When we use close() function it breaks the link from the disk so no
any task is possible.
Syntax:-
<file_handeler>.close()
Example :-
myfile.close()
Python provides many function for reading and writing the open filess.
Example :-
# First we create a text file which name is “demo_file.txt”, Its contain is---
# Python code for file handling is------(Reading a file and print it)
myfile=open('c:\\demo_file.txt',"r")
str=myfile.read()
print(str)
myfile.close()
myfile=open(r'c:\demo_file.txt',"r")
str=myfile.read()
print(str)
myfile.close()
myfile=open('c:\\demo_file.txt',"r")
str=myfile.read(10)
print(str)
myfile.close()
5