File Handling in Python

Contents show

File Handling in Python with Examples

File Handling in Python
File Handling in Python


1.1 Introduction

File Handling in Python


Most computer programs work with files. This because files help in storing information permanently. Word processors create document files ; Database program create files of information ; Compilers read source files and generate executable files. So, we see, it is the files of that are mostly worded with, inside programs. A file in itself is a bunch of bytes stored on some storage device like hard-disk, thumb-drive etc. Every programming language offers some provision to use and create files through programs. Python is no exception and in this chapter, you shall be learning to work with data files through Python programs.


1.2 Data Files


The data files are that store data pertaining to specific application, for later use. The data files can be stored in two ways :

  • Text files
  • Binary files


1. Text Files


A text file stores information in ASCII or Unicode characters (the one which is default for your programming platform).

1. In text files, each line of text is terminated, (delimited) with a specific character known as EOL (End of Line) character.

2. In text files, some internal translations take place when this EOL character isread or written.

3. In Python, by default, this EOL character is the newline character(‘/n’) or carriage-return, newline combination (‘/r/n’).


2. Binary Files


A binary files is just a file that contains information in the same format in which the information is held in memory, i.e., the file content that is returned to you is raw(with no translation or no specific encoding).

In binary files, there is no delimiter for a line. Also no translations occurs in binary files. As a result, binary files are faster and easier for a program to read and write than are text files.

As long as the file doesn’t need to be read by people or need to be ported to a different type of system,

binary files are the best way to store program information.


1.3 Opening and Closing Files


In order to work with a file from within a Python program, you need to open it in a specific mode as per the file manipulation task you want to perform. The most basic file manipulation tasks include adding, modifying or deleting data in a file, which in turn include any one or combination of the following operations :

  • reading data from files
  • writing data to files
  • appending data to files


Python provides built-in functions to perform each of these tasks. But before you can perform these functions on a file, you need to first open the file.


1.3.1 Opening Files


In data file handling through Python, the first thing that you do is open the file. It is done using open( ) function as per one of the following syntax’s :

<file_objectname> = open(<filename>)
<file_objectname> = open(<filename>, <mode>)

For example,

myfile = open("taxes.txt")        <------- Python will look for this file in current working directory

The above statement opens file “taxes.txt” in file mode as read mode (default mode) and attaches it to file object namely myfile.


Consider statement :

file2 = open("data.txt", "r")

The above statement opens file “data.txt” in read mode (because of “r” given as mode) and attaches it to file object namely file2.


Consider one more file-open statement :

file3 = open("e://main//result.txt", "w")       <----- Python will look for this file in E:/main folder

The above statement opens file “result.txt” (stored in folder E:/main) in write mode (because of “w” given as mode) and attaches it to file object namely file3.


The above given three file-open statements must have raised these questions in your mind :


(i) What is file-object ?

(ii) What is mode or file-mode ?


The coming lines will have answers to all your question :-

  • Python’s open( ) function creates a files object which serves as a link to a file residing on your computer.
  • The first parameter for the open( ) function is a path to the file you’d like to open. If just file name is given, then Python searches for the file in the current folder.
  • The second parameter of the open function corresponds to a mode which is typically read (‘r’), write (‘w’), or append (‘a’). If no second parameter is given, then by default it opens it in read (‘r’) mode.

1.3.1 A File Object/File Handle


File objects are used to read and write data to a file on disk.

The file object is usedto obtain a reference to the file on disk and open it for a number of different tasks.


1.3.1 B File Access Modes


When Python opens a files, it need to know the file-mode in which the file is being opened. A file-mode governs the type of operations (such as read or write or append) possible in the opened file i.e., it refers to how the file will be used once it’s opened.

File modes supported by Python are being given in Table.

Text File ModeBinary File ModeDescriptionNotes
‘r’‘rb’read only– File must exist already, otherwise Python raises I/O error.
‘w’‘wb’write only– If the file does not exist, file is created.
– If the file exists, Python will truncate existing data and over-write in the file. So this mode must be used with caution.
‘a’‘ab’append– File is write only mode.
– If the file exists, the data in the file is retained and new data being written will be appended to the end.
– If the file does not exist, Python will create a new file.
‘r+’‘r+b’ or ‘rb+’read and write– File must exist otherwise error is raised.
– Both reading and writing operations can take place.
‘w+’‘w+b’ or ‘wb+’write and read– File is created if does not exist.
– If file exists, file is truncated (past data is lost).
– Both reading and writing operations can take place.
‘a+’‘a+b’ or ‘ab+’write and read– File is created if does not exist.
– If file exists, file’s existing data is retained ; new data is appended.
– Both reading and writing operations can take place.
Table File-modes – File Handling In Python

To create a file, you need to open a file in a mode that supports write mode(i.e., ‘w’, or ‘a’ or ‘w+’ or ‘a+’ modes).

1.3.2 Closing Files

An open file is closed by calling the close( ) method of its file-object. Closing of file is important. In Python, files are automatically closed at the end of the program but it is good practice to get into habit of closing your files explicitly.

The close( ) function accomplishes this task and it takes the following general form :

<fileHandle>.close( )

For instance, if a file Master.txt is opened via file-handle outfile, it maybe closed by the following statement :

outfile.close( )       <------ The close( ) must be used with  
                             filehandle

Please remember, open( ) is a built-in function(used standalone) while close( ) is used with file-handle object.

1.4 Reading and Writing Files

Python provides many functions for reading and writing the open files. In this section, we are going to explore these functions. Most common file reading and writing functions are being discussed in coming lines.

1.4.1 Reading from Files

Python provides mainly three types of read functions to read from a data file. But before you can read from a file, the file mustbe opened and linked via a file-object or file handle. Most common file reading functions of Python are listed below in Table Python data files – reading writing functions.

S.NO.MethodSyntaxDescription
1.read( )<filehandle>.read([n])reads at most n bytes ; if no n is specified, reads the entire file.
Returns the read bytes in the form of a string.
2.readline( )<filehandle>.readline([n])reads a line of input ; if n is specified reads at most n bytes.
Returns the read bytes in the form of a string ending with In(line) character or returns a blank string if no more bytes are for reading in the file.
3.readlines( )<filehandle>.readlines( )reads all lines and returns them in a list.
The <filehandle> in above syntaxes is the file-object holding open file’s reference.

File Handling In Python

File Handling in Python Examples

Let us consider some examples now. For the examples and explanations below, we are using a file namely poem.txt storing the content shown in Fig. 5.2.

           WHY ?
We work, we try to be better
We work with full zest
But, why is that we just don't know any letter.
We still give our best.
We have to steal,
But, why is that we still don't get a meal.
We don't get luxury,
We don't get childhood,
But we still work,
Not for us, but for all the others.
Why is that some kids wear shoes, BUT we make them ?
by Mythili, class 5

Example 1. Reading a file’s first 30 bytes and printing it.

myfile = open(r'E:/poem.txt', "r")
str = myfile.read(30)
print(str)

The output produced by above code is :

>>>
WHY?
We work, we try

Example 2. Reading n bytes and then reading some more bytes from the last position read.

myfile = open(r'E:/poem.txt', 'r')
str = myfile.read(30)           # reading 30 bytes
print(str)
str2 = myfile.read(50)          #reading next 50 bytes
print(str2)
myfile.close( )

The output produced by above code is :

>>>
WHY?
We work, we try

to be better
We work with full zest
But, why is t

Example 3. Reading a file’s entire content.

myfile = open(r'E:/poem.txt', "r")
str = myfile.read( )
print(str)
myfile.close( )

The output produced by above code is :

WHY ?
We work, we try to be better
We work with full zest
But, why is that we just don't know any letter.
We still give our best.
We have to steal,
But, why is that we still don't get a meal.
We don't get luxury,
We don't get childhood,
But we still work,
Not for us, but for all the others.
Why is that some kids wear shoes, BUT we make them ?
by Mythili, class 5

Example 4. Reading a file’s first three lines – line by line

myfile = open(r'E:/poem.txt', "r")
str = myfile.readline( )
print(str, end = ' ')
str = myfile.readline( )
print(str, end = ' ')
str = myfile.readline( )
print(str, end = ' ')
myfile.close( )

The output produced by above code is :

>>>
WHY?     <--------- Line 1
                <-------- Line 2
We work, we try to be better  <------ Line 3

Example 5. Reading a complete file – line by line

myfile = open(r'E:/poem.txt', "r")
str = " "
while str :
       str = myfile.readline( )
       print(str, end = ' ')
myfile.close( )

The output produced by the above code will print the entire content of file poem.txt.

>>>
WHY ?
We work, we try to be better
We work with full zest
But, why is that we just don't know any letter.
We still give our best.
We have to steal,
But, why is that we still don't get a meal.
We don't get luxury,
We don't get childhood,
But we still work,
Not for us, but for all the others.
Why is that some kids wear shoes, BUT we make them ?
by Mythili, class 5

The readline( ) function reads the leading and trailing spaces (if any) along with trailing newline character(‘/n’) also while reading the line. You can remove these leading and trailing white spaces (spaces or tabs or newlines) using strip( ) (without any argument) as explained below.
Recall that strip( ) without any argument removes leading and trailing white-spaces.
There is another way of printing a file line by line. This is a simpler way where after opening a file you can browse through the file using its file handle line by line by writing following code :

<filehandle> = open(<filename>, [<any read mode>])
for <var> in <filehandle> :
      print(<var>)

For instance, for the above given file poem.txt, if you write following code, it will print the entire file line by line :

myfile = open(r'E:/poem.txt',  "r")
for line in myfile :
      print(line)

The output produced by code is just the same as the output produced by example 5. The reason behind this output is that when you iterate over a file-handle using a for loop.

Example 6. Displaying the size of a file after removing EOL character, leading and trailing white spaces and blank lines

myfile = open(r'E:/poem.txt', "r")
str1 = " "     #initially storing a space (any no-None value)
size = 0
tsize = 0
while str1 :
       str1 = myfile.readline( )
       tsize = tsize + len(str1)
       size = size + len(str1.strip() )
print("Size of file after removing all EOL character & blank lines :", size)
print("The Total size of the file :", tsize)
myfile.close( )

The output produced by above code fragment is :

Size of file after removing all EOL character & blank lines : 360
The Total size of the file : 387

All the above code fragment read contents from file in a string. However, if you use readlines( ) (notice ‘s’ in the end of the function), then the contents are read in a List. Go through next code fragment.

Example 7. Reading the complete file-in a list.

myfile = open(r'E:/poem.txt', "r")
s = myfile.readlines( )
print(s)
myfile.close( )

Now carefully look at the output. The readlines( ) has read the entire file in a list of strings where each line is stored as one string :

File Handling In Python programs

program 1. Write a program to display the size of a file in bytes.

myfile = open(r'E:/poem.txt', "r")
str = myfile.read( )
size = len(str)
print("Size of the given file poem.txt is")
print(size, "bytes")

Output

>>>
Size of the given file poem.txt is
387 bytes

Program 2. Write a program to display the number of lines in the file.

myfile = open(r'E:/poem.txt', "r")
s = myfile.readlines( )
linecount = len(s)
print("Number of lines in poem.txt is", linecount)
myfile.close( )

Output

Number of lines in poem.txt is 18

1.4.2 Writing onto Files


After working with file-reading functions, let us talk about the writing functions for data files available in Python. (see Table below). Like reading functions, the writing functions also work on open files, i.e., the files that opened and linked via a file-object or file-handle.

Table : Python Data Files – Writing Functions

S.NO.NameSyntaxDescription
1.write( )<filehandle>.write(str1)writes str1 to file referenced by <filehandle>
2.writelines( )<filehandle>.writelines(L)writes all strings in List L as lines to file referenced by <filehandle>
The <filehandle> in above syntaxes is the file-object holding open file’s reference – File Handling In Python

Appending a File


When you open a file in “w” or write mode, Python overwrites an existing file or creates a non-existing file. That means, for an existing file with the same name, the earlier data gets lost. If, however, you want to write into the file while retaining the old data, then you should open the file in “a” or append mode. A file opened in append mode retains its previous data while allowing you to add newer data into. You can also add a plus symbol(+) with file read mode to facilitate reading as well as writing.

File Handling In Python


That means, in Python, writing in files can take place in following forms :

(i) In an existing file, while retaining its content
(a) if the file has been opened in append mode (“A”) to retain the old content.
(b) if the file has been open in ‘r+’ or ‘a+’ modes to facilitate reading as well as writing.


(ii) to create a new file or to write on an existing file after truncating/ overwriting its old content
(a) if the file has been opened in write-only mode (“W”)
(b) if the file has been open in ‘w+’ mode to facilitate writing as well as reading


(iii) Make sure to use close( ) function on file-object after you have finished writing as sometimes, the content remains in memory buffer and to force-write the content on file and closing the link of file-handle from file, close( ) is used.

Let us consider some examples now.

Example 8. Create a file to hold some data.
fileout = open("Student.dat", "w")
for i in range(5) :
      name = input("Enter name of student :")
      fileout.write(name)
fileout.close( )

The sample run of above code is as shown below :

>>>
Enter name of student : Riya
Enter name of student : Rehan
Enter name of student : Ronaq
Enter name of student : Robert
Enter name of student : Ravneet

File Handling in Python Examples

Example 9. Create a file to hold some dat, separated as lines
(This code is creating different file than created in example 8)

fileout = open("Student.dat", "w")
for i in range(5) :
      name = input("Enter name of student :")
      fileout.write(name)
      fileout.write('/n')           #The newline character '/n' written after every name
fileout.close( )

The sample run of above code is as shown below :

>>>
Enter name of student : Jaya
Enter name of student : Jivin
Enter name of student : Jonathan
Enter name of student : Jagjeet
Enter name of student : Javed

Example 10. Creating a file with some names separated by newline character without using write( ) function.


(For this, we shall use writelines( ) inplace of write( ) function writes the content of a list to a file. Function writelines( ) also, does not add newline character, so you have to take care of adding newlines to your file.)
fileout = open("Student3.txt", "w")
List1 = [ ]
for i in range(5) :
      name =  input("Enter name of student :")
      List1.append(name+ '/n')   #Responsibility to add newline character is of programmer's
fileout.writelines(List1)
fileout.close( )

Sample run of above code is as shown below :

>>>
Enter name of student : Nitya
Enter name of student : Noor
Enter name of student : Nathan
Enter name of student : Naved
Enter name of student : Navin

1.4.3 The flush Function

File Handling In Python


When you write onto a file using any of the write functions, Python holds everything to write in the file in buffer and pushes it onto actual file on storage device a later time. If however, you want to force Python to write the contents of buffer onto storage, you can use flush( ) function.
Python automatically flushes the files when closing them i.e., this function is implicitly called by the close( ) function But you may want to flush the data before closing any file. The syntax to use flush( ) function is :

<fileobject>.flush( )

Consider the following example code :

f = open('out.log', 'w+')
f.write('The output is /n')
f.write("My" + "work-status" + "is")
f.flush( )

s = 'OK.'
f.write(s)
f.write('/n')
# some other work
f.write('Finally Over/n')
f.flush( )
f.close( )

1.4.4 Removing Whitespaces after Reading from File

The read( ) and readline( ) functions discussed above, read data from file and return it in string from and the readlines( ) function returns the entire file content in a list where each line is one item of the list.

All these read functions also read the leading and trailing whitespaces i.e., spaces of table or newline character. If you want to remove any of these trailing and leading whitespaces, you can use strip( ) functions [rstrip( ), lstrip( ) and strip( )] as shown below.

Recall that :

  • the strip( ) removes the given character from both ends.
  • the rstrip( ) removes the given character from trailing end i.e., right end.
  • the lstrip( ) removes the given character from leading end i.e., left end.

To understand this, consider the following examples :

1. Removing EOL ‘/n’ character from the line read from the file.

fh = file("poem.txt", "r")
line = fh.readline( )
line = line.rstrip('/n')

2. Removing the leading whitespaces from the line read from the file

line = file("poem.txt", "r").readline( )
line = line.lstrip( )

Now you can justify the output of following code that works with first line of file poem.txt shown above where first line containing leading 8 spaces followed by word ‘WHY?’ and a ‘/n’ in the end of line.

1.4.5 Significance of File Pointer in File Handling

Every file maintains a file pointer which tells the current position in the file where writing or reading will take place. (A file pointer in this context works like a book-mark in a book).

Whenever you read something from the file or write onto a file, then these two things happen involving file-pointer :

(i) this operation takes place at the position of file-pointer and

(ii) file-pointer advances by the specified number of bytes

1.4.5 A File Modes and Opening Position of File-Pointer

The position of a file-pointer is governed by the filemode. it is opened in. Following table lists the opening position of a file-pointer as per file mode.

File ModesOpening position of file-pointer
r, rb, r+, rb+, r+bbeginning of the file
w, wb, w+, wb+, w+bbeginning of the file (Overwrites the file if the file exists).
a, ab, a+, ab+, a+bat the end of the file if the file exists otherwise creates a new file.
Table : file modes and opening position of file-pointer – File Handling In Python

1.5 Standard Input, Output and Error Streams (File Handling In Python)

If Someone asks you to give input to a program interactively or by typing, you know what device you need for it – the keyboard. Similarly, if someone says that the output is to be displayed, you know which device it will be displayed on – the monitor. So, we can safely say that the keyboard is the standard input device and the monitor is standard output device. Similarly, any error if occurs is also displayed on the monitor. So, the monitor is also standard error device. that is,

1. standard input device (stdin) – reads from the keyboard

2. standard output device (stdout) – prints to the display and can be redirected as standard input.

3. standard error device (stderr) – Same as stdout but normally only for errors. Having error output separately allows the user to divert regular output to a file and still be able to read error messages.

Following code fragment shows you interesting use of these. It prints the contents of a file on monitor without using print statement :

import sys
fh = open(r"E:/poem.txt")
line1 = fh.readline( )
line2 = fh.readline( )
sys.stdout.write(line1)
sys.stdout.write(line2)
sys.stderr.write("No errors occured/n")

Output

>>> =======================
>>>
         WHY ?
We work, we try to be better
No errors occured
>>>

Absolute and Relative Paths

Full name of a file or directory or folder consists of path/primaryname.extension.

Path is a sequence of directory names which give you the hierarchy to access a particular directory or file name. Let us consider the following directory structure :

File Handling in Python Absolute and Relative Paths :

Absolute and Relative Paths
A Sample Directory Structure

File Handling in Python Examples

Read More :-

Recursion In Python 3 Tutorial

File Handling in Python

In this Tutorial File Handling In Python complete topics are covered with File Handling In Python examples.