File Handling in Python
Most computer programs work with files. This is because files help in storing
information permanently. A file in itself is a bunch of bytes stored on some
storage device like Hard Disk Drive (HDD).
A=10 Data File
B=40 Output
C=A+B Addition = 50
Output
Print(“addition=”,C
Addition = 50
RAM (Volatile Nature)
HDD (Non-Volatile)
Data file:
The data files are the files that store data pertaining to a specific application, for
later use. The data files can be stored in two ways.
Text file: A text file stores information in the form of a stream of ASCII or
Unicode characters. In text files, each line of text is terminated, (delimited) with a
special character known as EOL (End of Line) character.
In Python, by default, this EOL character is the newline character ( \n ) or carriage-
return, newline combination ( \r,\n).
1. Regular Text File: These are the text files which store the text in the same
form as typed. These tiles have a file extension as .txt. Ex. I am simple text.
2. Delimited Text file: A specific character is stored to separate the values.
Tab Separated Values (TSV): I am simple text.
Comma Separated Value (CSV): I, am, simple, text.
Binary File: A binary file stores the information in the form of a stream of bytes.
In binary file, there is no delimiter for a new line. Also no translations occur in
binary files. As a result, binary files are faster and easier for a program to read and
write than are text file.
Opening and Closing Files:
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.
Reading data from file
Writing data to files
Appending data to file
Opening File: open() // built-in function for open file
Syntax: <file_objectName>= open(<fileName>)
For Example: myfile= open(“demo.txt”)
<file_objectName> = open(<filename>, <mode>)
f= open(“c\\temp\\data.txt”,’r’)
Closing File: close() // built-in function for closing file
Note:
1. Double the slashes. ( f=open(“C:\\temp\\data.text”,”r”)
2. Give raw string by prefixing the file-path string with an r .
( f=open( r “C:\temp\data.text”,”r”)
Example: 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)
print(str)
str2=myfile.read(50)
print(str2)
myfile.close()
File Modes in Python
When opening a file, we must specify the mode we want to which
specifies what we want to do with the file. Here’s a table of the different
modes available:
Mode Description Behavior
r Read-only mode. Opens the file for reading. File must exist; otherwise, it raises
an error.
rb Read-only in binary Opens the file for reading binary data. File must exist;
mode. otherwise, it raises an error.
r+ Read and write mode. Opens the file for both reading and writing. File must exist;
otherwise, it raises an error.
rb+ Read and write in binary Opens the file for both reading and writing binary data. File
mode. must exist; otherwise, it raises an error.
w Write mode. Opens the file for writing. Creates a new file or truncates the
existing file.
wb Write in binary mode. Opens the file for writing binary data. Creates a new file or
truncates the existing file.
w+ Write and read mode. Opens the file for both writing and reading. Creates a new file
or truncates the existing file.
wb+ Write and read in binary Opens the file for both writing and reading binary data. Creates
mode. a new file or truncates the existing file.
a Append mode. Opens the file for appending data. Creates a new file if it
doesn’t exist.
ab Append in binary mode. Opens the file for appending binary data. Creates a new file if
it doesn’t exist.
a+ Append and read mode. Opens the file for appending and reading. Creates a new file if
it doesn’t exist.
ab+ Append and read in Opens the file for appending and reading binary data. Creates a
binary mode. new file if it doesn’t exist.
File name: 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.
File handling Programme (Reading from text file)
1. 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)
print(str)
str2=myfile.read(50)
print(str2)
myfile.close()
2. Reading a file’s entire content
myfile=open(r’E:\poem.txt’,’r’)
str=myfile.read()
print(str)
myfile.close()
3. Reading a file’s first three lines, line by line.
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
str=myfile.readline()
print(str)
str=myfile.readline()
print(str)
str=myfile.readline()
print(str)
str=myfile.readline()
print(str)
myfile.close()
4. Reading a complete file, line by line using while loop.
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
str=" "
while str:
str=myfile.readline()
print(str)
myfile.close()
5. Reading a complete file, line by line using for loop.
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
for line in myfile:
print(line)
myfile.close()
6. Displaying the size of a file after removing EOL characters, leading
and trailing white spaces and blank line.
Note: You can remoe these leading and trailing white spaces (spaces, tab,
Newline) using strip()
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
str=" "
size=0
tsize=0
while str:
str=myfile.readline()
tsize=tsize+len(str)
size=size+len(str.strip())
print("Size of file after removing all EOL character and blank lines:",size)
print("the Total Size of the file:",tsize)
myfile.close()
7. Reading the complete file in a list
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
s=myfile.readlines()
print(s)
myfile.close()
8. Write a program to display the size of a file in bytes.
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
str=myfile.read()
size=len(str)
print("Size of the given file Poem.txt is ")
print(size, "bytes")
myfile.close()
9. Write a program to display the number of lines in the file.
myfile=open(r'D:\python programme\FileHandling\Poem.txt', 'r')
str=myfile.readlines()
linecount=len(str)
print("Number of lines in poem.txt", linecount)
myfile.close()
Writing onto text files:
After working with file-reading functions, let us talk about the writing function for
data files available in Python.
Python Data Files- Writing Functions:
1. write(): Write string str to file referenced by file objects <filehandle>. It is
clear from the file-contents that writer() does not add any extra character
like newline character(\n) after every write operation.
2. writelines(): writes all strings in list as line to file referenced by file
objects.
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.
That means, in Python , writing in files can take place in following forms:
1. 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.
2. 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.
3. 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.
Note:
To fix this issue, you can either:
1. Use Python 3.x, where input() behaves as expected and returns a string.
2. Use raw_input() instead of input() in Python 2.x. raw_input() returns a string
without trying to evaluate it as Python code.
Example 1: Create a file to hold some data.
fileout = open("D:\\python programme\\FileHandling\\student.txt", "w")
for i in range(3):
name = raw_input("Enter name of student: ")
fileout.write(name)
fileout.write('\n')
fileout.close()
Example 2: Create a file with some names separated by new line
characters without using write() function.
fileout = open("D:\\python programme\\FileHandling\\student1.txt", "w")
List1=[ ]
for i in range(3):
name = raw_input("Enter name of student: ")
List1.append(name+"\n")
fileout.writelines(List1)
fileout.close()
Example 3: write a program to get roll number, names and marks of
the students of a class (get from user) and store these details in a file
called “Marks.txt”.
count = int(raw_input("How many students are there in the class? "))
fileout = open("D:\\python programme\\FileHandling\\Marks.txt", "w")
for i in range(count):
print("Enter details for student", (i+1), "below")
rollno = int(raw_input("Roll Number: "))
name = raw_input("Name: ")
marks = float(raw_input("Marks: "))
rec = str(rollno) + "," + name + "," + str(marks) + '\n'
fileout.write(rec)
fileout.close()
4. Write a program to add two more student’s details to the file created
in program example-3.
Note: Notice the file is opened in append mode (“a”) this time so as to retain old
content.
fileout = open("D:\\python programme\\FileHandling\\Marks.txt", "a")
for i in range(2):
print("Enter details for student", (i+1), "below")
rollno = int(raw_input("Roll Number: "))
name = raw_input("Name: ")
marks = float(raw_input("Marks: "))
rec = str(rollno) + "," + name + "," + str(marks) + '\n'
fileout.write(rec)
fileout.close()
5. Write a program to display the contents of file “Marks.txt”
created through programs ex.3 and ex.4.
fileout = open("D:\\python programme\\FileHandling\\Marks.txt", "r")
while str:
str=fileout.readline()
print(str)
fileout.close()
6. write a program to read a text file and display the count of
vowels and consonants in the file.
myfile = open("D:\\python programme\\FileHandling\\Poem.txt", "r")
ch=" "
vCount=0
cCount=0
while ch:
ch=myfile.read(1) #one character read from file
if ch in ['a','A','e','E','i','I','o','O','u','U']:
vCount = vCount+1
else:
cCount = cCount+1
print("Vowels in the file:", vCount)
print("Consonants in the file:", cCount)
myfile.close()
The flush() Function:
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 file buffers when closing them that is this
function implicitly called by the close() function. But you may want to flush the
data before closing any file.
Removing Whitespaces after reading from file:
Whitespaces spaces,tabs,newline
1. strip(): the strip() removes the given character from both ends.
2. rstrip(): Removes the given character from right end.
3. lstrip(): Removes the given character from left end.
File Pointer in File Handling:
Every file maintains a file pointer (cursor) which tells the current position in the file
where writing or reading will take place. Where 1 character = 1 byte (8 bits or 2 Nibble)
File Modes Opening position of file pointer
r, rb, r+, rb+, r+b Beginning of the file
w, wb, w+, wb+, w+b Beginning of the file ( Overwrites the file if the file exists).
a, ab, a+, ab+, a+b At the end of the file if the file exists otherwise create a new file.
Standard Input, Output and Error Streams:
1. (stdin): The keyboard is the standard input device .
2. (stdout): The Monitor is the standard output device.
3. (stderr): Any error if occurs is also displayed on the monitor. So the monitor is
also standard error device.
4. In Python, you can use these standard stream files by using sys module. Ex.
sys.stdin.read().
Ex. It prints the contents of a file on monitor without using print statement:
import sys
myfile = open("D:\\python programme\\FileHandling\\Poem.txt", "r")
line1=myfile.readline()
line2=myfile.readline()
line3=myfile.readline()
sys.stdout.write(line1)
sys.stdout.write(line2)
sys.stdout.write(line3)
sys.stderr.write("No errors occurred\n")
with Statement:
The with statement will automatically close the file after the nested block of code.
The advantage of using a with statement is that it is guaranteed to close the file
no matter how the nested block exits.
with open(“output.txt”,’w’) as f:
f.write(‘hi there’)
Even if an exception (a runtime error) occurs before the end of block, the with
statement will handle it and close the file.
Absolute and Relative Path:
Absolute Path: An absolute path is a complete path to a file or directory that
starts from the root directory of the file system. It provide the exact location of
the file or directory, including all the directories and subdirectory.
Ex. C:\users\username\documents\file.txt
Relative Path: A relative path is a path to file or directory that is relative to the
current working directory. It does not start form the root directory, but rather
from the current directory.
Ex- .\file.txt (current directory)
..\file.txt (Parent directory)
The symbol .(dot) can be used in place of current directory and ..(two dots)
denotes the parent directory.
Working with Binary file
Serialisation (Pickling):
Serialisation is the process of converting Python object hierarchy into a byte
stream so that it can be written into a file. Pickling converts an object in byte
stream in such a way that it can be reconstructed in original form when unpickled
or de-serialised.
Unpickling is the inverse of Pickling where a byte stream is converted into an
object hierarchy. Unpickling produces the exact replica of the original object.
In order to work with the pickle module, you must first import it in your
program using import statement. Ex. import pickle
dump(): Write an object to a binary file.
load(): Reads stream of bytes from a binary file.
Create/Opening/Closing Binary Files:
A binary file is opened in the same way as you open any other file, but make sure
to use ‘b’ with file modes to open a file in binary mode.
Ex. File1=open(‘stu.bat’,’wb+’)
An open binary file is closed in the same manner as you close any other file.
Ex. File1.close()
Writing onto a Binary File: dump()
Example 1: Write a program to a binary file called emp.dat and write into it
the employee details of some employees, available in the form of
dictionarys.
import pickle
# Employee details in the form of dictionaries
employees = [
{"emp_id": 1, "name": "John Doe", "age": 30, "department": "Sales"},
{"emp_id": 2, "name": "Jane Smith", "age": 28, "department": "Marketing"},
{"emp_id": 3, "name": "Bob Johnson", "age": 35, "department": "IT"}
]
# Write employee details to a binary file
with open("D:\\python programme\\emp.dat", "wb") as file:
pickle.dump(employees, file)
print("Employee details written to emp.dat successfully.")
Read employee details from the binary file
import pickle
with open("D:\\python programme\\emp.dat", "rb") as file:
employees = pickle.load(file)
for employee in employees:
print(employee)
File Methods:
1. file.fileno(): Returns an integer number (file descriptor) of the file.
2. file.seek(offset, from=SEEK_SET): Change the file position to offset bytes,
in reference to from (start, current, end).
3. file.tell(): Returns the current file location.
Ex. 1-
f1=open(‘abc.txt’,’w’
Str=”Python programming language”
f1.write(str)
Print(‘this file number is:’,f1.fileno())
f1.close()
Ex. 2-
str1 = "python programming is fun"
f1 = open('D:\\python programme\\test.txt', 'w')
f1.write(str1)
f1.close()
f1 = open('D:\\python programme\\test.txt', 'r+')
s = f1.read(10)
print('Read string is :', s)
pos = f1.tell()
print('Current file position:', pos)
pos = f1.seek(7, 0)
s = f1.read(10)
print('Again read string is:', s)
f1.close()