top
Easter Sale

Search

Python Tutorial

Python's built-in functions input() and print() perform read/write operations with standard IO streams. The input() function reads text into memory variables from keyboard which is defined as sys.stdin and the print() function send data to display device identified as sys.stdout. The sys module presents definitions of these objects.Instead of standard output device, if data is saved in persistent computer files then it can be used subsequently. File is a named location in computer’s non-volatile storage device such as disk. Python's built-in function open() returns file object mapped to a file on the permanent storage like disk.open() functionFile object is returned by open() function which needs name of file along with its path and file opening mode.file = open(name, mode) The mode parameter decides how the file is to be treated. Default mode is 'r' which means that it is now possible to read data from the file. To store data in it, mode parameter should be set to ‘w’. Other supported values of mode parameter and their significance are listed in following table: characterpurposerOpens a file for reading only. (default)wOpens a file for writing only, deleting earlier contentsaOpens a file for appending.topens file in text format (default)bOpens a file in binary format.+Opens a file for simultaneous reading and writing.xopens file for exclusive creation.The open() function returns a file like object representing any stream such a file, byte stream, socket or pipe etc. The file object supports various methods for operations on underlying data stream.write() methodFollowing statement opens python.txt in write mode.>>> f=open("python.txt","w")Next we have to put certain data in the file. The write() method stores a string in the file.>>> f.write(("Monty Python's Flying Circus")Make sure that you close the file object in the end.>>> f.close()The "python.txt" is now created in current folder. Try opening it using any text editor to confirm that it contains above text.writelines() methodThe file object also has writelines() method to write items in a list object to a file. The newline characters ("\n)  should be the part of the string.lines=[" Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n"]f=open("python.txt","w") f.writelines(lines) f.close()The python.txt shows following data. When opened with editor.Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.read() methodTo read data from a file, we need to open it in ‘r’ mode.>>> f=open('python.txt','r')The read() method reads certain number of characters from file's current reading position. To read first 9 characters in file:>>> f.read(9) 'Beautiful'readline() methodThis method reads current line till it encounters newline character.>>> f=open('python.txt','r') >>> f.readline() 'Beautiful is better than ugly.\n'To read file line by line until all lines are read,f=open("python.txt","r") while True:        line=f.readline()        if line=='':break        print (line) f.close()readlines() method : This method reads all lines and returns a list object.>>> f=open('python.txt','r') >>> f.readlines() ['Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n']Exception in File HandlingFile operation is subject to occurrence of exception. If file could not be opened, OSError is raised and if it is not found, FileNotFoundError is raised.>>> f=open("anyfile.txt","r") Traceback (most recent call last):  File "<pyshell#8>", line 1, in <module>    f=open("anyfile.txt","r") FileNotFoundError: [Errno 2] No such file or directory: 'anyfile.txt'Hence such operations should always be provided exception handling mechanism.try:        f=open("python.txt","r")        while True:                line=f.readline()                if line=='':break                print (line, end='') except FileNotFoundError:        print ("File is not found") else:        f.close()File object as iteratorThe file object is a data stream that supports next() method to read file line by line. When end of file is encountered, StopIteration exception is raised.f=open("python.txt","r") while True:        try:                line=next(f)                print (line, end="")        except StopIteration:                break f.close()Output:Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.append mode When a file is opened with "w" mode its contents are deleted if it exists already. In order to add more data to existing file use "a" mode (append mode). f=open("python.txt","a") If additional data is now written, it gets added to the end of file. >>> f=open('python.txt','a') >>> f.write("Flat is better than nested.\n") 28 >>> f.close()The file will have additional line at the end.+ mode for simultaneous read/write "w" mode or "a" mode allows data to be written into and cannot be read from. On the other hand "r" mode allows reading only but doesn't allow write operation. In order to perform simultaneous read/write operations, use "r+" or "w+" mode.seek() methodThe seek() method of file object sets current read/write position to specified location in the file.f.seek(offset, whence) Here the whence parameter counts offset from beginning, current position or end respectively:0 : offset calculated from beginning1 : offset calculated from current position2 : offset calculated from end.Let us assume that python.txt contains following lines.Beautiful is better than ugly. Explicit is better than implicit.We now try to replace the word 'better' with 'always better'. First open the file in read/write mode. Place the file pointer at the beginning of 2nd line and read it. The replace 'better' with 'always better' and rewrite the line.Code:f=open("python.txt","r+") f.seek(32,0) s=f.readline() s=s.replace('is', 'is always') print (s) f.seek(32,0) f.write(s) f.seek(0,0) lines=f.readlines() for line in lines:        print (line) f.close()Output:Beautiful is better than ugly. Explicit is always better than implicit.Binary fileThe open() function opens a file in text format by default. To open file in binary format add ‘b’ to mode parameter. Hence "rb" mode opens file in binary format for reading and "wb" mode opens file in binary format for writing. Unlike text mode files, binary files are not human readable. When opened using any text editor, the data is unrecognizable.Following code stores a number in a binary file. It is first converted in a bytes before writing. The function to_bytes() of int class returns byte representation of the object.f=open('number','wb') d=1024 f.write(int.to_bytes(d,16, 'big')) f.close()To read above binary file, output of read() method is casted to integer by from_bytes() method. f=open('number','wb') d=1024 f.write(int.to_bytes(d,16, 'big')) f.close()File attributes:In addition to above methods, file object is also characterized by following attributes:AttributeDescriptionfile.closedReturns true if file is closed, false otherwise.file.modeReturns access mode with which file was opened.file.nameReturns name of the file.
logo

Python Tutorial

Python - FileIO

Python's built-in functions input() and print() perform read/write operations with standard IO streams. The input() function reads text into memory variables from keyboard which is defined as sys.stdin and the print() function send data to display device identified as sys.stdout. The sys module presents definitions of these objects.

Instead of standard output device, if data is saved in persistent computer files then it can be used subsequently. File is a named location in computer’s non-volatile storage device such as disk. Python's built-in function open() returns file object mapped to a file on the permanent storage like disk.

open() function

File object is returned by open() function which needs name of file along with its path and file opening mode.

file = open(name, mode) 

The mode parameter decides how the file is to be treated. Default mode is 'r' which means that it is now possible to read data from the file. To store data in it, mode parameter should be set to ‘w’. Other supported values of mode parameter and their significance are listed in following table: 

characterpurpose
rOpens a file for reading only. (default)
wOpens a file for writing only, deleting earlier contents
aOpens a file for appending.
topens file in text format (default)
bOpens a file in binary format.
+Opens a file for simultaneous reading and writing.
xopens file for exclusive creation.

The open() function returns a file like object representing any stream such a file, byte stream, socket or pipe etc. The file object supports various methods for operations on underlying data stream.

write() method

Following statement opens python.txt in write mode.

>>> f=open("python.txt","w")

Next we have to put certain data in the file. The write() method stores a string in the file.

>>> f.write(("Monty Python's Flying Circus")

Make sure that you close the file object in the end.

>>> f.close()

The "python.txt" is now created in current folder. Try opening it using any text editor to confirm that it contains above text.

writelines() method

The file object also has writelines() method to write items in a list object to a file. The newline characters ("\n)  should be the part of the string.

lines=[" Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n"]

f=open("python.txt","w")
f.writelines(lines)
f.close()

The python.txt shows following data. When opened with editor.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

read() method

To read data from a file, we need to open it in ‘r’ mode.

>>> f=open('python.txt','r')

The read() method reads certain number of characters from file's current reading position. To read first 9 characters in file:

>>> f.read(9)
'Beautiful'

readline() method

This method reads current line till it encounters newline character.

>>> f=open('python.txt','r')
>>> f.readline()
'Beautiful is better than ugly.\n'

To read file line by line until all lines are read,

f=open("python.txt","r")
while True:
       line=f.readline()
       if line=='':break
       print (line)
f.close()

readlines() method : 

This method reads all lines and returns a list object.

>>> f=open('python.txt','r')
>>> f.readlines()
['Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n']

Exception in File Handling

File operation is subject to occurrence of exception. If file could not be opened, OSError is raised and if it is not found, FileNotFoundError is raised.

>>> f=open("anyfile.txt","r")
Traceback (most recent call last):
 File "<pyshell#8>", line 1, in <module>
   f=open("anyfile.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'anyfile.txt'

Hence such operations should always be provided exception handling mechanism.

try:
       f=open("python.txt","r")
       while True:
               line=f.readline()
               if line=='':break
               print (line, end='')
except FileNotFoundError:
       print ("File is not found")
else:
       f.close()

File object as iterator

The file object is a data stream that supports next() method to read file line by line. When end of file is encountered, StopIteration exception is raised.

f=open("python.txt","r")
while True:
       try:
               line=next(f)
               print (line, end="")
       except StopIteration:
               break
f.close()

Output:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

append mode 

When a file is opened with "w" mode its contents are deleted if it exists already. In order to add more data to existing file use "a" mode (append mode). 

f=open("python.txt","a") 

If additional data is now written, it gets added to the end of file. 

>>> f=open('python.txt','a')
>>> f.write("Flat is better than nested.\n")
28
>>> f.close()

The file will have additional line at the end.

+ mode for simultaneous read/write 

"w" mode or "a" mode allows data to be written into and cannot be read from. On the other hand "r" mode allows reading only but doesn't allow write operation. In order to perform simultaneous read/write operations, use "r+" or "w+" mode.

seek() method

The seek() method of file object sets current read/write position to specified location in the file.

f.seek(offset, whence) 

Here the whence parameter counts offset from beginning, current position or end respectively:

  • 0 : offset calculated from beginning
  • 1 : offset calculated from current position
  • 2 : offset calculated from end.

Let us assume that python.txt contains following lines.

Beautiful is better than ugly.
Explicit is better than implicit.

We now try to replace the word 'better' with 'always better'. First open the file in read/write mode. Place the file pointer at the beginning of 2nd line and read it. The replace 'better' with 'always better' and rewrite the line.

Code:

f=open("python.txt","r+")
f.seek(32,0)
s=f.readline()
s=s.replace('is', 'is always')
print (s)
f.seek(32,0)
f.write(s)
f.seek(0,0)
lines=f.readlines()
for line in lines:
       print (line)
f.close()

Output:

Beautiful is better than ugly.
Explicit is always better than implicit.

Binary file

The open() function opens a file in text format by default. To open file in binary format add ‘b’ to mode parameter. Hence "rb" mode opens file in binary format for reading and "wb" mode opens file in binary format for writing. Unlike text mode files, binary files are not human readable. When opened using any text editor, the data is unrecognizable.

Following code stores a number in a binary file. It is first converted in a bytes before writing. The function to_bytes() of int class returns byte representation of the object.

f=open('number','wb')
d=1024
f.write(int.to_bytes(d,16, 'big'))
f.close()

To read above binary file, output of read() method is casted to integer by from_bytes() method. 

f=open('number','wb')
d=1024
f.write(int.to_bytes(d,16, 'big'))
f.close()

File attributes:

In addition to above methods, file object is also characterized by following attributes:

AttributeDescription
file.closedReturns true if file is closed, false otherwise.
file.modeReturns access mode with which file was opened.
file.nameReturns name of the file.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Eula

This is my first time here. I am truly impressed to read all this in one place.

Jaypee Dela Cruz

Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!

lucky verma

Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.

Pre Engineered Metal Building

Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.

Pre Engineered Metal Building

Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More