Linux

Python File Input/Output

We have seen examples of programs that have taken input data from users at the keyboard. With Python, it is easy to read strings from plain text files. You can store data in text file. You know very well text file text file are platform independent.

Reading Text from a File

In order to read and write a file, Python built-in function open() is used to open the file. The open() function creates a file object.
Syntax
 
file object = open(file_name [, access_mode])
First argument file_name represents the file name that you want to access.
Second argument access_mode determines, in which mode the file has to be opened, i.e., read, write, append, etc.
Access_mode for reading, is 'r' . The program reads a simple text file i created on my system using text editor. Here are the contents of the file.
A Python txt file
simple_file.txt
I saved the file with the name simple_file.txt and put it in the same directory for easy access.
file_input = open("simple_file.txt",'r')
all_read = file_input.read()
print all_read
file_input.close()
Out-put
Reading file
Reading characters from the file by using read(count).
Let us discuss in next example
file_input = open("simple_file.txt",'r')
print file_input.read(5)
print file_input.read(10)
file_input.close()
Out-put
file reading by Python read(count)
Showing read(count) function
Second line
file_input.read(5)
reads first 5 characters third line reads next 10 characters

Reading line by line to do this use readline() function. Let us discuss in next example.
file_input = open("simple_file.txt",'r')
print file_input.readline()
print file_input.readline()
file_input.close()
Out-put
Python Reading file line by line
showing readline()
What happen if you specify count value in readline() function like this readline(count ). In this situation it read character by character as read in read(count) function.

What happen it I use readlines() function.
Let us discuss in next example.
file_input = open("simple_file.txt",'r')
print file_input.readlines()
file_input.close()
Out-put
Python readlines()
readlines() function
So it retrun a list of lines.

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code.
f= open("simple_file.txt", 'r')
for line in f:
   print line
Out-put
Python for loop prints the file
for loop prints the file


Writing Text to a File

In this section we will discuss write a file with Python. This time we will use write mode "w" in open() function.
We will use write() function.
Let us discuss our example.
text_file = open("writeit.txt",'w')
text_file.write("My name is Mohit")
text_file.write("\nI am a C|EH")
text_file.write("\nI am also E|CSA")
text_file.close()
Out-put
Python text_file.write()
Program write a writeit.txt
Above program write 3 lines in the text file name writeit.txt file.
You can write all 3 lines in single function like this text_file.write("My name is Mohit \n I am a C|EH \n I am also E|CSA")

Next I create same file using writelines() function. This method writes a list of strings to a file. In this i create a list of strings to be written.
line =["My name is Mohit \n",
"I am a C|EH \n",
"I am also E|CSA"]
text_file = open("writenew.txt", 'w')
text_file.writelines(line)
Out-put
Python writelines() write file
writelines() write file

Here is a list of the different modes of opening a file.
Modes Description
rOpens a file for reading only. This is default mode. This mode places the pointer in begining of file.
rbOpens a file for reading only in binary formate. This mode places the pointer in begining of file.
r+Opens a file for reading and writing. This mode places the pointer in begining of file.
rb+Opens a file for reading and writing only in binary formate. This mode places the pointer in begining of file.
wOpens a file for writing only. Create the file if it doesn't exist. If file exists overwrite the file.
wbOpens a file for writing only in binary mode. Create the file if it doesn't exist. If file exists overwrite the file.
w+Opens a file for reading and writing. Create the file if doesn't exist. If file doesn't exists overwrite the file.
wb+Opens a file for reading and writing in binary mode. Create the file if it doesn't exists. If file exists overwrite the file.
aOpens a file for appending mode. If file doesn't exist it creates new file, if file already exists pointer is placed at end of the file
abOpens a file for appending in binary mode. If file doesn't exist it creates new file, if file already exists pointer is placed at end of the file
a+Opens a file for appending and reading. If file doesn't exist it creates new file, if file already exists pointer is placed at end of the file
ab+Opens a file for appending and reading in binary mode. If file doesn't exist it creates new file, if file already exists pointer is placed at end of the file

Let us discuss a+ mode.
text_file = open("simple_file.txt",'a+')
text_file.write("Python is best")
text_file.write("\nTime saving")
text_file.write("\nrun faster")
text_file.close()
Out-put
Python append() using a+
Showing 'a+' mode
Above example is self-explanatory




admin