Python File close() Method

0

Python File close() Method

  • A file which is opened should be closed using the close() method.
  • Once a file is opened but not closed, then the data of the file may be corrupted or deleted in some cases.
  • If the file is not closed, the memory utilized by the file is not freed, leading to problems like insufficient memory. This happens when we are working with several files simultaneously.
  • After completing our operations on the file,it is highly recommended to close the file. For this we have to use close() function.  
  • You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

Syntax

  • f.close()
  • file.close()

Various properties of File Object:

Once we opened a file and we got file object,we can get various details related to that file by using its properties.
  • name → Name of opened file 
  • mode → Mode in which the file is opened 
  • closed → Returns boolean value indicates that file is closed or not 
  • readable() → Returns boolean value indicates that whether file is readable or not 
  • writable() → Returns boolean value indicates that whether file is writable or not. 

Eg:

Source Code:

#Follow @Programmerfect

f=open("abc.txt",'w')
print("File Name: ",f.name)
print("File Mode: ",f.mode)
print("Is File Readable:  ",f.readable())
print("Is File Writable:  ",f.writable())
print("Is File Closed : ",f.closed)
f.close()
print("Is File Closed : ",f.closed) 

Output:



 


Post a Comment

0Comments
Post a Comment (0)