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 |
r | Opens 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. |
a | Opens 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.
Attribute | Description |
file.closed | Returns true, if file is closed, else false. |
file.mode | Returns access mode of the file. |
file.name | Returns name of the file. |
Example-
- # Open a file
- fm = open("foo.txt", "wb")
- print("Name of the file: ", fm.name)
- print("Closed or not : ", fm.closed)
- 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-
- # Open a file
- fm = open("my.txt", "w")
- print("Name of the file: ", fm.name)
- # Close opend file
- fm.close()
Output-
Writing in to file
We can write into our file, using write() function.
Syntax-
fileObject.write(string)
Example-
- # Open a file
- fm =open("my.txt", "w")
- fm.write( "c# corner is world's number one website/nhaving million's of user and increasing day by day")
- # Close opend file
- fm.close()
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-
- # Open a file
- str1=''
- fm = open("my.txt", "r")
- for item in fm.read():
- str1=str1+item
- print("file contains: ",str1)
- # Close opend file
- 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-
- # Open a file
- str1=''
- fm = open("my.txt", "r")
- p=fm.tell()
- print("current position",p)
- fm.close()
Output-
seek()
seek() function is used to change the current file position.
Syntax-
- # Open a file
- str1=''
- fm = open("my.txt", "r")
- p=fm.tell()
- print("position before seek",p)
- fm.seek(20)
- po=fm.tell()
- print("position after seek",po)
- 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-
- import os
- 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-
- import os
- 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.