I/O Files In Python

In this chapter, we will learn how to store our data permanently into the disk.
In previous chapter, we learnt, how to read or write our data into a variable. It means our data is temporary saved on RAM and after closing of the program, variable is destroyed from RAM.
Storing of data into physical disk can be done, using I/O files functions.

We will discuss these functions in detail with the examples.

Opening and closing of file

Before we perform operations in the file we must to create File object using open function.

Open() function

Before reading or writing a file, we must create a file object. Open() create file object.

Syntax-

file object = open(file_name [, access_mode][, buffering])

file_name:
The file_name is name used to save the file.
access_mode: The access_mode is a mode in which the file is opened, i.e. read, write, append etc.
buffering: If the buffering value is 0, no buffering takes place. If the buffering value is 1, line buffering is performed, while accessing a file.
 
Modes

Modes Description
rOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. 
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. 
r+ Opens a file for both reading and writing. The file pointer is placed at the beginning of the file. This is default mode.
rb+Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file. 
w  Opens a file for writing only. It overwrites the file, if the file exists. If the file does not exist, it creates a new file for writing.
wb Opens a file for writing only in binary format. It overwrites the file if the file exists. If the file does not exist, creates a new file for writing. 
w+ Opens a file for both writing and reading. Overwrites the existing file , if the file exists. If the file does not exist, it creates a new file for reading and writing. 
wb+Opens a file for both writing and reading in binary format. Overwrites the existing file, if the file exists. If the file does not exist, it creates a new file for reading and writing.
aOpens a file for appending. The file pointer is at the end of the file if the file exists. The file is in the append mode. If the file does not exist, it creates a new file for writing. 
ab Opens a file for appending in binary format. The file pointer is at the end of the file, if the file exists. The file is in the append mode. If the file does not exist, it creates a new file for writing. 
a+ Opens a file for both appending and reading. The file pointer is at the end of the file, if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. 
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file, if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. 
 file Object Attributes

These operators work on open file.

AttributeDescription 
file.closedReturns true, if file is closed, else false.
file.modeReturns access mode of the file. 
file.nameReturns name of the file. 

Example-
  1. # Open a file  
  2. fm = open("foo.txt""wb")  
  3. print("Name of the file: ", fm.name)  
  4. print("Closed or not : ", fm.closed)  
  5. print("Opening mode : ", fm.mode)  
Output-
 

close() Method

Python automatically closes file but it is a good practice to close the file with close() function.

Syntax-

fileObject.close()

Example-
  1. # Open a file  
  2. fm = open("my.txt""w")  
  3. print("Name of the file: ", fm.name)  
  4. # Close opend file  
  5. fm.close()  
Output-


 
Writing in to file

We can write into our file, using write() function.

Syntax-

fileObject.write(string)

Example-
  1. # Open a file  
  2. fm =open("my.txt""w")  
  3. fm.write( "c# corner is world's number one website/nhaving million's of user and increasing day by day")  
  4. # Close opend file  
  5. fm.close()  
String is written into the file and file is automatically saved at the location of our Python program, when the file is saved.

Open a file in Notepad and the same string is found.
 
Reading of file

Reading of file is done using read() function.

Syntax-

fileObject.read([count])

Example-
  1. # Open a file  
  2. str1=''  
  3. fm = open("my.txt""r")  
  4. for item in fm.read():  
  5. str1=str1+item  
  6. print("file contains: ",str1)  
  7. # Close opend file  
  8. fm.close()  
Output-
 
Accessing file (file position)

In Python programming language, we can access the file, using built-in function.

Tell() or seek() function.

Tell() function is used to know the current position in the file.

Syntax-

variable=fo.tell()

Example-
  1. # Open a file  
  2. str1=''  
  3. fm = open("my.txt""r")  
  4. p=fm.tell()  
  5. print("current position",p)  
  6. fm.close()  
Output-

seek()

seek() function is used to change the current file position.

Syntax-
  1. # Open a file  
  2. str1=''  
  3. fm = open("my.txt""r")  
  4. p=fm.tell()  
  5. print("position before seek",p)  
  6. fm.seek(20)  
  7. po=fm.tell()  
  8. print("position after seek",po)  
  9. fm.close()  
Output-
 

Renaming and Deleting

In Python programming, we can rename the file, using “os” module built-in function.

First, import os module and then use built-in function.

Rename of text file-

Rename of text file can be done, using os.rename() function.

Syntax-

Os.rename(“old_filename.txt”,”new_filename.txt”)

Example-
  1. import os  
  2. os.rename("c#corner.txt","my.txt")  
Delete the text file-

Deleting of a text file can be done, using os.remove() function.

Syntax-

os.rename(“text_filename.txt”)


Example-
  1. import os  
  2. os.remove("my.txt")  
Summary

In this chapter, you will learn how to create a file, save into the hard disk memory and how to access them.