IKH

Introduction

  • As the part of programming requirement, we have to store our data permanently for future purpose. For this requirement we should go for files.
  • Files are very common permanent storage areas to store our data.
  • There are two types of files:
    • Text files
    • Binary files

Text Files

  • Usually we can use text files to store character data.
  • Example – abc.txt

Binary Files

  • Usually we can use binary files to store binary data.
  • Example – images, video, audio files …etc.

Opening a File

  • Before performing any operation (like read or write) on the file, first we have to open that file. For this we should use python’s inbuilt function open(). But at the time of open, we have to specify mode, which represents the purpose of opening file.

Syntax

Python
f = open(filename, mode)
  • The allowed modes in python are:
    • r ⇒ Open an existing file for read operation. The file pointer is positioned at the beginning of the file. If the specified file does not exist then we will get FileNotFoundError. This is default mode.
    • w ⇒ Open an existing file for write operation. If the file already contains some data then it will be overridden. If the specified file does not exist then this mode will create that file.
  • a open an existing file for append operation. It won’t override existing data.If the specified file is not already avaialble then this mode will create a new file.
  • r+ To read and write data into the file. The previous data in the file will not be deleted.The file pointer is placed at the beginning of the file.
  • w+ To write and read data. It will override existing data.
  • a+ To append and read data from the file.It wont override existing data.
  • x To open a file in exclusive creation mode for write operation. If the file already exists then we will get FileExistsError.

Note

  • All the above modes are applicable for text files. If the above modes suffixed with ‘b’ then these represents for binary files.

Example

Python
rb,wb,ab,r+b,w+b,a+b,xb
f = open("abc.txt","w")
We are opening abc.txt file for writing data.

Output

PowerShell
5

Closing a File

  • After completing our operations on the file,it is highly recommended to close the file. For this we have to use close() function.
  • f.close()

Various properties of File Object

  • Once we opend 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() Retruns boolean value indicates that whether file is readable or not
  • writable() Returns boolean value indicates that whether file is writable or not.

Example

Python
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
D:\Python_classes>py test.py 
File Name: abc.txt 
File Mode: w 
Is File Readable: False 
Is File Writable: True 
Is File Closed : False 
Is File Closed : True

Output

PowerShell
7

Report an error