You are on page 1of 56

Data File Handling

There are 2 types of programs:


Transient - They run for a short time and produce some output, but when they end, their data
disappears, because the data entered is executed inside primary memory, RAM, which is
volatile(temporary) in nature.
Persistent - They run for a long time and keep at least some of their data in permanent storage
(hard disk).

File
Files are named locations on disk to store related information. They are used to permanently store
data in a non-volatile memory (e.g. hard disk).
or
A file is a stream or sequence of characters/bytes (sequence of related data) occupying a named
place on the disk.

INTRODUCTION
 FILE HANDLING is a mechanism by which we can read data of disk files in python
program or write back data from python program to disk files.
 So far in our python program the standard input is coming from keyboard an output is
going to monitor i.e. nowhere data is stored permanently and entered data is present as long
as program is running. BUT file handling allows us to store data entered through python
program permanently in disk file and later on we can read back the data.
** EOL: End of Line
read(): returns a string read from the file.
read(n): If n is not specified or negative value for n is specified as size, then the entire file will
be read.
readline(): For readline(), a line is terminated by ‘\n’.
////
** For storing data with end of line character, we will have to add ‘\n’ character to the end
of the string, and it is of 2 bytes.
If we do not provide \n the entire data will be written to a single line.
** If file does not exists in the current directory, a new file is created.
** If file exists, data present in the file shall be overwritten every time we run the program.
* for storing numeric data value in a text file, conversion to string is required.
Example:
>>>x=52
>>>file1.write (str(52))
** writelines() accepts any type of sequence as an argument.
** In writelines() also we have to add \n explicitly.
Random Access in files
Python allows random access of the file data using built-in functions:
1. tell()
2. seek()

tell() function : is used to get the actual position of file object/handle. By file object/handle we
mean a cursor. And it’s cursor, who decides from where data has to be read or written in a file.
Syntax:
f.tell(), where f is the file handle/object.
Returns: returns the position of file object/handle.

seek(): It is used to shift/change the position of file object to required position. By file object we
mean a cursor. And it’s cursor, who decides from where data has to be read or write in a file.

Syntax:
f.seek(offset, from_what), where f is file pointer.

Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
The reference point is selected by the from_what argument. It accepts three values:

 0: sets the reference point at the beginning of the file

 1: sets the reference point at the current file position

 2: sets the reference point at the end of the file

By default from_what argument is set to 0.


Note: Reference point at current position / end of file cannot be set in text mode except when
offset is equal to 0.
seek() function with negative offset only works when file is opened in binary mode.

test.txt

Program:
Output:
File Opening modes:
Difference between r and r+ in open()

A text file for testing.


Read and write a file with r+
In r+ mode, we can read and write the file, but the file pointer position is at the beginning of the
file; if we write the file directly, it will overwrite the beginning content.

See the below example:

The below example uses f.read() to move the file pointer to the end of the file, and append a new
line.
Difference between w and w+ in open()

Write a file with w+


Read and write a file with w+

Difference between a and a+ in open()

A text file for testing.

Append a file with a+


Difference between r+ and w+ in open()

 If the file does not exist, r+ throws FileNotFoundError; the w+ creates the file.
 If the file exists, r+ opens it without truncating; the w+ truncates the file and opens it.
Difference between r+ and a+ in open()
 If the file does not exist, r+ throws FileNotFoundError; the a+ creates the file.
 For r+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the
initial file pointer position at the end of the file.
Difference between w+ and a+ in open()
 If the file exists, w+ truncates the file and opens it; a+ opens it without truncating.
 For w+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the
initial file pointer position at the end of the file.

Absolute vs. relative path

A path to an entity (in this case, a file, folder, or web page) describes the entity’s unique location
within a hierarchical directory or website structure.

Paths can be of two types. They can either be absolute or relative.

Example of a directory structure


Absolute Path

The absolute path (also known as the full path) of an entity contains the complete information
(from the root to the ending) needed to locate it. The absolute path is not affected by the user’s
current working directory, and it always includes the root directory as a starting point.

The absolute path to the Macaroni file is: C:\Recipes\Pasta\Macaroni.

While the absolute path to the CheeseCake file is: C:\Recipes\Cakes\CheeseCake.

Note: Here, C:\ is the root directory, and C:\Recipes is the current working directory.

Absolute paths are easier to understand, but they can be inconvenient to work with as each step
from the root to the entity needs to be included.

Relative Path

The relative path of an entity contains the information needed to locate that entity relative to the
user’s current working directory. The relative path disregards the information needed to locate the
current working directory from the root directory and only focuses on the route from the working
directory to the entity.
Since the relative path uses the current working directory as a vantage point, the Macaroni file’s
relative path is Pasta\Macaroni and the relative path to the CheeseCake file is Cakes\CheeseCake.
Note how we are simply studying the file paths relative to the current working
directory, C:\Recipes.

Although relative paths hold less information than absolute paths, they are shorter and easier to
work with (especially in deeply nested directories).

Test.txt
We are students of class 12 D.
We all are together.
We all are good.

Prog 1: To count the number of lines that starts with ‘W’.


Output:

Prog 2: To count the number of times ‘are’ occurs in file.

Output:
Prog 3: Menu-Driven Program
Output:
Prog 1: Write a program to write the below 2 rows to a binary file “Student.dat”.
[23,’John’,12]
[25,’Mira’,13]

Prog 2:
Write a program to write Name and Roll No into a binary file “file.dat”. The data should be in
the given format:
(Name, Roll No)
Prog 3:
Write a program to write [code,name,amount] into a binary file “customer.dat”, where the data is
taken from the user.
Prog 4:
Write a program to read all the contents from the binary file “file.dat”. The file has data as a list
[name,rollno]

Output: First without using try and except


Second when we use try and except
Prog. To delete a record from “customer.dat” file where name is “Seema”.
Updating record in a binary file

Prog: Update the salary of customer in “demo.dat” file whose name is “Anu”.

Prog: Update the salary of customer in “demo.dat” file where city is “Delhi”.

import pickle
f=open('customer.dat','wb')
pickle.dump(['C101','Nidhi','Delhi',2000],f)
pickle.dump(['C102','Manisha','Pune',3000],f)
f.close()
Prog:
Select and print all rows where city is “salary” is greater than 2000 from “customer.dat” file.
Data is stored in this format:[City,Cust Name, Amount]

Output:

Using the same template you can apply various selection crieteria:
1. Select and print sum of salaries where city is “Delhi” from “demo.dat” file.
Data is stored in this format:[City,Cust Name, Amount]

Output:

Prog : Program to count total number of rows in “customer.dat” file.

Output:
Prog : Program to copy one file into another file.

Selective read from binary file:

Prog 1:
Write a program to display only student name from binary file “file.dat”. The file has data as list
(name,rollno).
Output:

Prog 2:
Select and print all rows where city is “Delhi” from “customer.dat” file.
Data is stored in this format:[City,Cust Name, Amount]

Output:
* For working with CSV files in Python, there is an in-built module called csv. It is used to
read and write tabular data in CSV format.
r- raw data

Output

Python program to print records in the form of comma seperated values.


Output
Number of Records in file = 8

# To count exact number of records in Student.csv file.


import csv
f=open('Student.csv','r')
csv_reader=csv.reader(f)
count=0
for row in csv_reader:
if csv_reader.line_num==0:
continue
count=len(list(csv_reader))
print('Number of records:',count)
f.close()
line_num is nothing but a counter which returns the number of rows which have been
iterated.

# Program to search the record of a particular student from csv file on the basis of inputted
name.
import csv
f=open('Student.csv','r')
csv_reader=csv.reader(f)
name=input('Enter the name that you want to search')
for row in csv_reader:
if row[1]==name:
print(row)
f.close()
Python program to print records in the form of comma seperated values.

# Program to write one record at a time in csv file using writerow function
import csv
f=open('Demo.csv','w')
csv_w=csv.writer(f)
csv_w.writerow(['Name','Age'])
csv_w.writerow(['Anu',16])
csv_w.writerow(['Tanu',17])
f.close()
Output:
** To suppress EOL character, use newline=’’ while opening file.

import csv
f=open('Demo.csv','w',newline='')
csv_w=csv.writer(f)
csv_w.writerow(['Name','Age'])
csv_w.writerow(['Anu',16])

csv_w.writerow(['Tanu',17])
f.close()
# use of writerows()

import csv

f=open('Demo.csv','w',newline='')

values=[['Name','Age'],['Anu',16],['Tanu',17]]

csv_w=csv.writer(f)

csv_w.writerows(values)

f.close()

You might also like