Skip to main content

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc).
When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name.
By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well.
To open a file, you specify its name and indicate whether you want to read or write. Opening a file creates a file object. In this example, the variable ‘f ‘ refers to the new file object.
>>> f = open("test.dat","w")
This will create a file test.dat in the current directory. If the file already exists, it will overwrite.
The open function takes two arguments. The first is the name of the file, and the second is the mode. Mode "w" means that we are opening the file for writing. The following are common file opening modes.
r- for reading
r+- reading and writing
a+-appending and reading
w+-writing and reading
rb-read a binary file
wb-writing a binary file
rb+-reading and writing a binary file
wb+-writing and reading a binary file
ab+-appending or reading a binary file

>>> print f
<open file 't.dat', mode 'w' at 0x018B4DE0>
When we print the file object, we see the name of the file, the mode, and the location of the object.
Note: The open command will open a file in the current directory. If we want to open a file in some other directory, we have to specify complete path to the file.

Writing data into file
To put data in the file we invoke the write method on the file object after opening the file in write mode:
>>> f = open("test.dat","w")
>>> f.write("Python Programming\n")
>>> f.write("It is great")
>>> f.close()

Closing the file tells the system that we are done writing and makes the file available for  reading:
Now test the file content test.dat by opening it any editor(notepad(windows),gedit,vi etc(linux)).
The file method write expects a string as an argument. Therefore, other types of data, such as integers or floating-point numbers, must first be converted to strings before being written to an output file. In Python, the values of most data types can be converted to strings by using the str function. The resulting strings are then written to a file with a space or a newline as a separator character.
The following code will write numbers 1-10  line by line in the file num.dat
for i in range(1,11):

The argument of write has to be a string, so if we want to put other values in a file, we have to convert them to strings first. The easiest way to do that is with the str function:
An alternative is to use the format operator %.
>>> cars = 52
>>> "In July we sold %d cars." % cars
’In July we sold 52 cars.’
>>> "In %d days we made %f million %s." % (34,6.1,’dollars’)
’In 34 days we made 6.100000 million dollars.’

When the file is opened in write(w) mode and if the file already exist , write command will overwrite the contents.If you want to add contents to an existing file, the file must be opened in append(a)mode. The following command will open the ‘test.dat’ file and append the new contents.
>>>f.write(“this is the new content”)
The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
>>>Lst=[“this is a test\n”,”on writelines method in python\n”,”file handling”]
Reading data from a file
For reading data from a file, first open the file in read mode
If we try to open a file that doesn’t exist, we get an error:
>>> f = open("","r")
IOError: [Errno 2] No such file or directory: ’’
The read() method will read the entire contents of the file as string.
>>> print l
Python Programming
It is great

Note that once the contents are read with read() method, the file pointer is moved to the end of file. The read() method will then return an empty string. If you want to read it again move the file pointer to the beginning of the file using seek() command.
All reading and writing functions discussed till now, work sequentially in the file. To access the contents of file randomly - seek and tell methods are used.
tell() method returns an integer giving the current position of object in the file. The integer returned specifies the number of bytes from the beginning of the file till the current position of file object.It's syntax is
seek()method can be used to position the file object at particular place in the file. It's syntax is : [, from_what])
here offset is used to calculate the position of fileobject in the file in bytes. Offset is added to from_what (reference point) to get the position. Following is the list of from_what values:
Value reference point
0 beginning of the file
1 current position of file
2 end of file
default value of from_what is 0, i.e. beginning of the file.

This will read 5 bytes from the file and the file pointer is advanced to the 6’th byte.

A text file is a file that contains printable characters and whitespace, organized into lines separated by newline characters. Since Python is specifically designed to process text files, it provides methods that make the job easy.

The readline() command can be used to read line by line from a file
>>> f = open("test.dat","w")
>>> f.write("line one\nline two\nline three\n")
>>> f.close()
The readline method reads all the characters up to and including the next newline
>>> f = open("test.dat","r")
>>> print f.readline()
line one
The file pointer will be advanced to next line after reading each line.
f.readlines() will  read all lines into a list.

we can also use a for loop which will read file line by line
for l in f:
            print l
with statement
You can also work with file objects using the with statement. It is designed to provide much cleaner syntax and exceptions handling when you are working with code. One bonus of using this method is that any files opened will be closed automatically after you are done. 
with open("file1.dat") as f:
            for line in f:
                        print line,
In order to put values into a file, you have to convert them to strings. You have already seen how to do that with str:
>>> f.write (str(12.3))
>>> f.write (str([1,2,3]))
The problem is that when you read the value back, you get a string. The original type information has been lost. In fact, you can’t even tell where one value ends and the next begins:
>>> f.readline()
’12.3[1, 2, 3]’
The solution is pickling, so called because it “preserves” data structures. The pickle module contains the necessary commands. To use it, import pickle and then open the file in the usual way:
>>> import pickle
>>> f = open("test.dat","w")
To store a data structure, use the dump method and then close the file in the usual
>>> pickle.dump(12.3, f)
>>> pickle.dump([1,2,3], f)
>>> f.close()
Then we can open the file for reading and load the data structures we dumped:
>>> f = open("test.pck","r")
>>> x = pickle.load(f)
>>> x
>>> type(x)
<type ’float’>
>>> y = pickle.load(f)
>>> y
[1, 2, 3]
>>> type(y)
<type ’list’>
Each time we invoke load, we get a single value from the file, complete with its original type.
Exception Handling
Whenever a runtime error occurs, it creates an exception. Usually, the program stops and  Python prints an error message.
Examples of different type of exceptions
Zero division error-division by zero
>>> 2/0
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
IndexError-index out of range
>>> a=[10,20,30]
>>> print a[3]
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    print a[3]
IndexError: list index out of range
>>> a={'a':10,'b':20}
>>> a['c']
Key error-use of non existent key
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
KeyError: 'c'
IO error-opening a non existing file
>>> f=open("","r")
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
IOError: [Errno 2] No such file or directory: ''
Note that Python prints a traceback of where the program was and also print  the error message with description.
Sometimes we want to execute an operation that could cause an exception, but we don’t want the program to stop. We can handle the exception using the try and except statements in Python
For example, we might prompt the user for the name of a file and then try to open it. If the file doesn’t exist, we don’t want the program to crash; we want to handle the exception:
filename = raw_input(’Enter a file name: ’)
f = open (filename, "r")
except IOError:
print ’There is no file named’, filename
The try statement executes the statements in the first block. If no exceptions occur, it ignores the except statement. If an exception of type IOError occurs, it executes the statements in the except branch and then continues.
You can write a function exist() which will return true if the file exist or False otherwise.
def exists(filename):
f = open(filename)
return True
except IOError:
return False
Note: We can also write multiple except blocks to handle different type of exceptions.
We can also raise an exceptions in a program by raise command. In this case the program stops after displaying the error message.try except block can also be used to handle the raise exceptions.
x=input("enter x")
y=input("enter y")
if y==0:
    raise ValueError,'bad input value zero'
    print x/y
Sample Programs
The following program will copy file1.dat to file2.dat 
f1 = open("file1.dat", "r")
f2 = open("file2.dat", "w")
while True:
  if l=="":
print 'File is copied'
The following program will copy file1.dat to file2.dat after removing lines starting with # (comment lines in python)
while True:
    if l=="":
    if l[0]!='#':
print 'File is copied'
The following program will copy file1.dat to file2.dat after removing blank lines
( university question)
f1 = open("file1.dat", "r")
f2 = open("file2.dat", "w")
lst=f1.readlines() # reading lines to a list
for l in lst:
    if l !='\n':
         f2.write(text)          # writing non empty lines to new file
print "File copied"
program that reads a file and writes out a file with the lines in reversed order.
( university question)
f1 = open("file1.dat", "r")
f2 = open("file2.dat", "w")
lst=f1.readlines() # read the entire contents into a list
rlst=reversed(lst) #reversing the list
f2.writelines(rlst) # writing each line in the new file
we can also do the above program by reversing the file object
f1 = open("file1.dat", "r")
f2 = open("file3.dat", "w")
rf1=reversed(list(f1)) # reversing the file object after converting it into a list sequence
for line in rf1:
    f2.write(line) # writing each line in the new file
print "File copied"
Read and print a file with all punctuation removed.( university question)
import string
f1 = open("file1.dat", "r")                                         #create the file file1.dat with punctuations
for line in f1:                                                              # read each line
    print line.translate(None,string.punctuation),            #translate punctuations to none
program ro read a text file and print each word and its length in sorted order
f=open("file1.dat","r")            #reading the file contents into a string
str=str.replace("\n"," ")   #replace all newline with spaces
words=str.split(" ")    #split the string into words
print "words and length"
for w in words:
    print w,len(w)
Create a file with 10 integer numbers and finding the average of odd numbers
#writing 10 numbers into a file
print "Enter 10 integers.."
for i in range(10):
    n=input()            # if you are using raw_input no need of str conversion
    f.write(str(n)+"\n") # writing numbers line by line
#reading numbers and finding average of odd numbers
for n in f:
      num=int(n) #converting string n to int num
      if num%2 !=0:
          s=s+num # finding sum of odd numbers
          c=c+1   # finding the count of odd numbers
print "the sum of odd numbers=",s,"average=",s/c
Write a program to read numbers stored in one file and store the sorted numbers in another file after deleting duplicates.(university question)
f1=open("file1.dat","r") # numbers are stored line by line
lst=f1.readlines()           # read the numbers into a list
nlst=list(set(lst))     # removing duplicates  easy method is to make a set
nlst.sort()              # sorting the list
f2=open("file2.dat","w") #writing into a new file
print "sorted file is created"
Finding the count of four letter words in a file ( University Question)
stn=st.replace("\n"," ")
words=stn.split(" ")
for w in words:
    if len(w)==4:
        print w
print "Count of 4 letter words=",c

Write a python program to create a file with 10 integer numbers. Read this file and display the square of each number. ( university question)
print "Enter 10 numbers..."
for i in range(10):
print "Numbers and its square.."
for i in f:
    print n,n**2
Reading numbers from a file and storing even and odd numbers in two separate files 
for i in l:
    if n%2==0:

print 'Files are created...'
Reading numbers from a file and storing positive and negative numbers in two separate files ( university question)
f1=open("num.txt","r")      # numbers are stored line by line
lst=f1.readlines()          # read the numbers into a list
print lst
for i in lst:  # seperating postive and negative numbers
    if int(i)<0:
f2=open("positive.txt","w") #writing into a new files
print "Files created"
Print prime numbers from a set of numbers stored in a file
def primetest(n):
  while i<=n/2:
    if n%i==0:
  return prime

 print "file does not exist"
while True:
  if n=="":
  if primetest(n):
Read name and mark in two subjects of a student and store it in a file. Display the name and sum of marks.( use pickling)
import pickle
name=raw_input("Enter the name...")
m1=input("mark in subject1..")
m2=input("mark in subject2...")
print "name=",namef,"total mark=",m1f+m2f
Python program to read two matrices from two files, find the sum and display the resultant matrix.Assume the first line of the input file represent the order of the matrix in a comma separated format and the remaining lines represent the rows of the matrix in a comma separated format.(university question)
f1=open("mat1.dat","r") # open the first matrix file mat1.dat
f2=open("mat2.dat","r") # open the second matrix file mat2.dat
first1=f1.readline() # read the first lines from both the files
first2=f2.readline() #and check the dimension
if m1 != m2 or n1 != n2:
   print "Dimension doesnt match cant add"
   print "Sum of two matrix" # reading each row and finding the  sum
   for i in range(m1):
       for j in range(n1):
       print r3
Write a Python program to store lines of text into a file.Read the file and display only the palindrome words in the file.(university question)
#creating  file
while True:
    ln=raw_input("Enter a line of text...type quit to exit\n")
    if ln=='quit':
#reading and printing palindrome words
lst=lst.replace("\n"," ")
words=lst.split(" ")
print "Palindrome words"
for w in words:
   if w==w[::-1]:
       print w


Try the following Programs in the lab
1.Input list of strings and create a text file with this sequence.
2.Copy one file to another
3.Copy one python program file to another after removing the single line comments
( lines starting with #)
4.Read and print a file with line numbers of each line.
5.Read and print a file with all punctuation removed.( university question)
6.Write a python program to create a file with 10 integer numbers. Read this file and display the square of each number. (University question)
7.Write a program that reads a file and writes out a file with the lines in reversed order.( university question-refer example)
8.Write a python program to read a text file, copy the content to another file after removing the blank lines.( university question)
9.Count the number of lines, number of words and number of characters in a file.(refer example)
10.Program to read a text file and print the words and its length in sorted order.( refer example)
11.Write a Python program that opens a file for input and prints the count of four letter words.( university question..)
12.write a program to read numbers stored in one file and store the sorted numbers in another file after deleting duplicates.(university question-refer example)
13.Write rollnumber(integer) name(string) and attendance percentage(float) of n students into a file. Read the file and show the list of students having attendance shortage(less than 75%).(use pickling)
14.Read a file and display the lines with number of words in each line. Use a function exist() which will handle an exception if the file does not exist.
15.Write a python program to read numbers from a file named num.txt.Write all positive numbers from num.txt to file named positive.txt and all negative numbers to negative.txt.(university question)
16.Write a python program to read two matrices from two files, find the sum and display the resultant matrix.Assume the first line of the input file represent the order of the matrix in a comma separated format and the remaining lines represent the rows of the matrix in a comma separated format.(university question)
17.Write a Python program to store lines of text into a file.Read the file and display only the palindrome words in the file.(university question)


  1. Really appreciated the information and please keep sharing, I would like to share some information regarding online training.Maxmunus Solutions is providing the best quality of this PYTHON programming language. and the training will be online and very convenient for the learner.This course gives you the knowledge you need to achieve success.

    For Joining online training batches please feel free to call or email us.
    Email :
    Contact No.-+91-9066638196/91-9738075708

  2. Thank you for sharing such great information. I am really impressed. Keep posting.
    Python training in Noida

  3. Awesome, very nice blog. I like it. thanks for sharing helpful post.
    Python Training Institute in Gurgaon

  4. Hi!
    Awesome post, really useful!

    Have you heard about Publish Green? You’ve probably seen our Ebooks all over the place. We’re the leading free Ebooks for the world. We’ve just launched our website Object Oriented Programming Python Pdf Pdf where we give away the best free Ebook resources out there. We’d be stoked if you could add us to this list.
    Keep sharing With us

  5. Well Done ! the blog is great and Interactive it is aboutThe Python Software Foundation is looking for bloggers! useful for students and Python Developers for more updates on python

    python online training

  6. Nice post. Thanks for sharing such basic information.
    Python training in Delhi

  7. Thank you. Truly appreciate all your hardwork, especially your down-to-earth attitude.

    Also Visit this site for "The Best Python Certification"

  8. It's a great post and useful to me. Thanks for Sharing.

  9. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Python Training in Bangalore

  10. nice blog,Thank you for sharing such a great article.keep sharing.
    python trainingpython online training

  11. thanks for sharing the information.Indian Cyber Army is announcing “ Summer Internship 2018” on “ Ethical hacking and Cyber Crime Investigation” for the enthusiasts of Cyber security. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization

  12. Thanks for the information.It is really nice .Information security is the set of processes that maintain the confidentiality, integrity and availability of business data in its various forms.In this age of Technology advancement, computer and information technology have not only brought convenience to citizens in modern life but also for policemen & various Government officials of the nation to fight cybercrime through various modus operandi. Indian Cyber Army has been dedicated in fighting cyber crime, striving to maintain law and order in cyberspace so as to ensure that everyone remains digitally safe.Read more:- Information Security

  13. This comment has been removed by the author.

  14. Nice blog this information is unique information i haven't seen ever by seeing this blog i came know lots of new things
    python training in Hyderabad the best career

  15. Really it was an awesome article...very interesting to read..
    Thanks for sharing..
    Best Software Training Centre in Chennai | Machine Learning Training in Chennai

  16. thanks for sharing the more valuable information.
    Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
    Ethical hacking course

  17. Thanks For sharing the more valuable information. we are clearly understanding this concept. we do have dedicated and experience developer for hire.Hire Python Developer


  18. Worthful Python tutorial. Appreciate a lot for taking up the pain to write such a quality content on Python tutorial. Just now I watched this similar Python tutorial and I think this will enhance the knowledge of other visitors for sure. Thanks anyway.:-

  19. Thanks for sharing the valuable information to share with us. For more information please visit our website. The right way to Learn Python Training in Ameerpet|| Get Learned

  20. If you ever wondered who are the most sophisticated players in the IT outsourcing industry, I'd highly recommend checking out this article to learn more about outsourced product development .


  21. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting!! Python Learning

  22. Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On Reading The article Python Online Course Hadoop Online Course Aws Online Course Data Science Online Course

  23. Hiiii....Thanks for sharing Great info....Nice post....Keep move on...
    Python Training in Hyderabad

  24. Your blog was excellent. Your blog is very much to useful for me, Thanks for shareing that information. Keep blogging
    Python Training in Electronic City


Post a Comment

Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language"""
This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’
We can also slice a string. Segment of a string is called a slice.
>>>s[1:3] ‘yt’
If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’
>>>s[2:6] ‘thon’
If the last index is not mentioned, the slicing will go till the end o…

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…