KEMBAR78
XII Computer Science Gist For Data Files Full | PDF | Computer File | Comma Separated Values
0% found this document useful (0 votes)
5 views26 pages

XII Computer Science Gist For Data Files Full

The document provides an overview of data file handling in Python, emphasizing the importance of data files for permanent storage of program output. It covers types of data files, file operations, methods for reading and writing data, and the use of file objects. Additionally, it explains binary file handling using the pickle module for storing and retrieving objects.

Uploaded by

Koushani Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

XII Computer Science Gist For Data Files Full

The document provides an overview of data file handling in Python, emphasizing the importance of data files for permanent storage of program output. It covers types of data files, file operations, methods for reading and writing data, and the use of file objects. Additionally, it explains binary file handling using the pickle module for storing and retrieving objects.

Uploaded by

Koushani Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Class XII

Computer Science
Gist of Data File Handling (Full)
Class XII: Working with Data Files (Python)
What is the utility of Data Files?
• The output of a program is normally shown on the screen
• The moment you terminate the program, the output is lost
• In case you need to permanently store the output of a program, you need Data Files
• Using Data Files you can:
▪ Store the output of a program permanently in a secondary storage device like a Hard Disk or any
similar device
▪ Read data from a Data File and use the data in your program for processing

Types of Data Files?


There are two types of Data Files: Text File & Binary File. The differences between them are given below:

File INPUT and OUTPUT Operations:

BASIC Modes and PLUS Modes of Opening a Data File:


Before Opening a File, Python needs to know the mode in which to open it. Modes determine the type of
operation i.e. read/write possible on a file.
OPENING a File:
To work with a file, the first thing to do is to OPEN the file. A file is opened in one of the following ways:

File Object serves as the LINK


between processor and HDD

Different forms of First Parameter (File Name and Path)


First parameter of file open( ), can have the following formats:
• open ( "employee.txt" )
File name only: File employee.txt present in same folder where program file saved
• open ( "D:\\temp\\employee.txt" )
File name with path: File present in path given. Note that double slash \\ used to prevent treating \ as
part of Escape Sequences like \n, \t, \r etc.
• open ( r "D:\temp\employee.txt" )
File name with path as raw string: Prefix r in front of file path string makes it a raw string which
removes any special meaning for any character. In this case \t as in \temp will not have any special
meaning attached to it (like tab character)
• Path = input('Enter file path: ')  file path is input as: D:\temp\employee.txt
open ( Path )
File name/path as a variable string: The filename along with path entered from user as a string and
used in the open( ) function. Most flexible form.
The File Object
The File Object:
• Is also called the File Handle
• Represents a Stream of Bytes using which the data can be read byte-by-byte or
line-by-line or as a whole using specific functions
• It thus serves as a link between the File stored on the Hardware and the Operating
System that runs the processor
• During a File Open operation using open( ) function, the reference of the opened file from the HDD is
stored in the file object
• File READ and WRITE operations and operations like data modification, data deletion, new data
insertion take place through the File Object
CLOSING a File:
• Though an opened file is automatically closed at the end of a program, but it is always a good practice
to close a file before closing the program
• The following syntax is used to close a file: Example: fileOut . close( )

Methods to READ Data from Files:


The following methods are used to read data from a data file:

Reading from Text File: Using read() method


Example-1: Program to use read() method to read data from the file ‘rhyme.txt’ under the current folder.

Text File when


opened by text
editor like
Program Output Notepad.
displaying first 4
characters read.

The read( ) method in the above code reads the text file up to 4 characters starting from the fist character,
stores it in variable s, and displays it.

Example-2: The entire content of the file can be read when using read() without any arguments.

Text File when


opened by text
editor like Notepad.

Program Output

• The read( ) method without any argument reads the entire text file and displays it line by line as it is
stored in the file
• Note the newline inserted at the end of each line, prints an extra space after the last line displayed
Example-3: Reading from Text File with path and file name input by user:

Text File when


opened by text
editor like Notepad.

Program
Output

Note that the path is entered by the user using a single back slash (root)
Example-4: When data is read from the file in instalments (reading partial data):

Text File when


opened by text
editor like Notepad.
Output
Program

• First 8 characters read into variable s1, and displayed


• Then the next 8 characters are read into variable s2, and displayed. Note, while counting, the newline
character ‘\n’ present at the end of each line as an invisible character, is counted as a single character
• Finally, the next 5 characters are read into variable s3, and displayed
How data is stored in Hard Disk & Viewed:
A text file when viewed using a text editor like Notepad or printed, looks like the one shown below:

The same file in reality is stored in the Hard Disk as a series of ASCII characters without any line break:

Note that the end of a line is marked by the newline character \n, which serves as the end of line marker (EOL).
Read operations using methods like readline( ) and readlines( ) work using this EOL marker.
Working of File Pointer during Read Operation:
Whenever a file is opened, in read mode, a special marker called a File Pointer (red arrow) positions itself at the
beginning of the file on the first character. As data is read from the file, the file pointer moves forward. After
reading n characters, the file pointer places itself after the last character read, i.e. at the n+1th character. The
next read operation starts from that point. A part of the text file is shown below:
Using read() method to read FULL FILE and process the read string
Example-5: The following program counts the total number of white space characters like space, newline, and
tab in the file PriceList.txt (the file contains blank spaces, tab characters and newline characters)
Here the entire file is read first and then the read string is analysed character by character using a for loop.

Text File
when
opened
by text
editor

Program Output

Using read(1) method to read data CHARACTER BY CHARACTER using a LOOP


Example-6: Using text file PriceList.txt to count and print the number of digits present in the file.
Here the file is read character by character using a loop. The read(1) code reads 1 character at a time from file.
NOTE: When the end of the file is reached, the read() method returns a Null String '' (i.e. an empty string with
ONLY two quotes). The if statement checks that to break out of the loop and stop reading data from file.

text editor
opened by
when
Text File
Program Output

Reading from Text File: Using readline() method to read entire line of text using a LOOP
• The readline( ) method will read a full line, along with the ending newline \n
• A line is read up to a new line character (EOL, i.e. \n, which prints a new line)
• In case an argument is used like readline(n), then the readline() method will read up to n characters or
up to the newline character \n, WHICHEVER COMES FIRST
• The function reads the leading & trailing spaces along with trailing newline
• Any leading and trailing spaces can be removed using the strip() method like s.strip()
Example-1: Program to read the file PriceList.txt and print the 4th character (at index 3) from each line.

Program Output
Example-2: The following program reads the file PriceList.txt and prints the 2nd word (at index 1) from each line.
For this the split() function works on the read line and splits it into a list
of strings, where each string is a word from the line.

Program Output

The following lists are created by the split() function for every line read. Each word forms an element of the list.
In this way words can be extracted from a line and processed individually.

LISTs formed for each line

Example-3 Using text file PriceList.txt to count line-wise, the number of vowels present in each line of the file.
Here the readline() method is used to read the file line by line.

Program Output
Reading from Text File: Using for LOOP to read data
• The for loop reads one line (up to EOL) at a time from the file object indicated by fin
• It will then store it in the loop variable line
• This string in line can then be printed or processed one by one as per requirement

Example-1: Program to read from the file rhyme.txt one line at a time using a for loop:

Text File
when opened
by text editor
like Notepad.

Program Output
Example-2: Using the text file rhyme.txt to get the maximum length line from the file.

Text File
when
opened by
text editor

Program
Output
Note that the line length here includes the invisible newline character at the end of each line.

Reading from Text File: Using readlines() to read data at one go from as text file
The readlines( ) method is used to read data from the file as a list. The file is read as a list with each line forming
an element of the list. A line is read up to the EOL i.e. \n, which is included in the string.

Example-1: Program to read the file rhyme.txt using readlines()


Text File
when
opened by
text editor

Program
Output

Note that an extra blank line is printed after each line due to the newline character at the end of each line.

Example-2: To print the first 4 characters of the text file rhyme.txt, where file is opened using readlines().

Program Output
Text File
when
opened by
text editor

Example-3: To print the length of each line of the text file rhyme.txt, where file is opened using readlines().

Program Output
Methods to WRITE Data (File Output) to TEXT Files:
There are two methods for writing data to a text file. These are write() and writelines(). There is no writeline()

Output Data to Text File using write( ) method and storing string data in the file:
Example-1: The program creates a file colours.txt in the temp folder under the E drive. Each colour name is input
from the user and written to the file.
The EOL marker ‘\n’ is to be added at the end of each string input before it is written to the file.

File Data

Program
Output

Output Data to Text File using write( ) method and storing numeric data:
Example-2: The following program creates a file cube.txt in the temp folder under the E drive.
The file stores the cube of all the numbers from 1 to n. Note how the numeric value of the cube (x**3) is converted
to a string using the str() function str(x**3) before it can be written to the file.

Program
Output

Text File
created,
when opened
by text editor
Output Data to Text File using write( ) method for storing both string and numeric data:
Example-3: Program to create a file elements.txt to store name, symbol, and atomic number of a set of elements.
The data for each element is stored in a comma separated manner similar to a csv (comma separated values) file.

Text File created,


when opened by
text editor

Note that the integer data is converted to a string using the


str() function, when creating the string rec to be written to the
file. Also note the addition of the newline character at the end
of each string. The commas are added to separate each data
for a given line. Hence you can create a csv file using the write()
function also.

Program Output

Appending Numeric Data to File using write( ) and ‘a’ mode


Ex-1: Program to append data to the existing file cube.txt file.

Before Append

Text File
created,
when
After Append opened
The append mode adds the new data at the end of the file, in text
while preserving the original data in the file. editor
Ex-2: Program to append data to the existing colours.txt file:

Before
Append
After
Append
Output Data to Text File using writelines( ) method for storing both string and numeric data:
Program to create a file names.txt in the temp folder under the E drive to store a set of names (string) and age
(numeric) values from the user. Note how the numeric data is converted to string and joined together with
string data before it is appended to the list L, inside the loop. The final list is written to the file OUTSIDE the
loop using the writelines() method.

Each element in the list will be written as a line of text in the text file, with a EOL character \n ending the line.

Text File
created,
when opened
by text editor
Binary Data Files
The methods used to write and read data from a file are available under the pickle module. The following methods
are used to read and write data to a binary file
 dump( )
▪ Used to write data to a binary file. Each dump() operation writes ONLY 1 OBJECT to a binary file
▪ Process is called pickling
▪ Example: pickle.dump( data_object, file_object )
 load( )
▪ Used to read data to a binary file. Each load() operation reads ONLY 1 OBJECT from a binary file
▪ Process is called un-pickling
▪ Example: data_object = pickle.load( file_object)

Writing Data to Binary File using dump() method: Numeric Data


The binary file throws.dat stores the outcome of throwing two dice together for n number of throws.
NOTE that the number will be written as a number only and NOT converted to any other type or text.
Example-1: Program to write numbers representing dice throws to a binary file using dump() method.

In the above code the loop runs n number of times and the randint(1,6) function generates a value between 1
and 6 randomly (emulating the throw of a dice) and stores the values in the variables d1 and d2. The sum of
these values is stored in variable d, and the number d is dumped to the file using the load function and the file
object. Note that the number is written as a number and not converted to text. The output of the binary file is
shown above, along with the output of the program. as discussed earlier, it is in a non-readable format.
Reading Data from Binary File using load() method: Numeric Data
Example-2: Program to read numbers representing dice throws from a binary file using load() method and count
the number of dice values above 7.

The code that can generate an error is written under a try block. When error occurs, program control gets
transferred to the except block.
When the end of the file is reached, the load() method raises an error as it is unable to read data from the file. If
the error is not taken care of, the program will stop working and show the traceback error message. To prevent
this, the file read code using the load() method is written within the try block.
When the end of the file is reached and the load() method is unable to read further data, the program control
gets transferred to the except block, where as a corrective action, the file is closed.
EOFError is the error name for End Of File Errors.
In the above code the while loop executes the load() function to read one number at a time from the file. With
each run of the loop a single number is read from the file. The number read is stored in the variable d. The if
statement next checks if d is more than 7 or not. If so, it prints the value of d. Note that the number is read
using the load() function as a number and need not be converted to a number like a text file.
The output is shown besides the code and it can be noted that the numbers printed are all more than 7.

Writing Data to Binary File using dump() method: String Data


The binary file employees.dat stores the names of employees for n number of employees.
NOTE that the string will be written as a string only.
Example-3: Program to write names of employees to a binary file using dump() method.

In the above code the loop runs n number of times and inputs n names
from the user, which are then one by one dumped to the file using the
load() function and the file object. Note that the name is written as a
string and not converted to any other format. No new line ‘\n’ is added
to the end of the text. The output of the binary file is shown above, along
with the output of the program to store 5 names.
Reading Data from Binary File using load() method: String Data
Example-4: Program to read the names of employees from a binary file using load() method and print those
names which are more than 2 words in length.
In the above code the while loop executes the load() function to read one name at a time from the file. With
each run of the loop a single name is read from the file. The name read is stored in the variable name. The split()
function is then used to split the name at the blank spaces and creates a list L with each word in the name
forming an element of the list L.
The if statement next checks if the length of the list, (which indicates the number of words in the name), is more
than 2 or not. If so, it prints the name. Note that the name is read using the load() function as a string.
Writing Data to Binary File using dump() method: LIST Data
The binary file employee.dat stores the name, age, and salary of a set of employees, where each employee record
is stored in a list. These lists are written into the file.
NOTE that the list will be written as a list only and NOT converted to any other type.
Example-5: Program to write lists to a binary file using dump() method

Program
Output

• The mode to use is ‘wb’ to write data to a binary file


• The list data need not be converted to string type data and is stored with their original data types
• No need of any newline character ‘\n’ to indicate end of line

Reading Data from Binary File using load() method: LIST Data
Example-6: To read data i.e., each list from the binary file employee.dat we have created above and print the
name of those employees whose age is more than 40.

Program
Output

• The mode to use is ‘rb’ to read data from a binary file


• The integer data in L[1] i.e. age, is directly compared as an integer and need not be converted to an integer
ALLWAYS USE THE try except MECHANISM TO READ DATA FROM A BINARY FILE

Writing Data to a Binary File using dump() method: Dictionary Data


Ex-7: The binary file student.dat stores the name, class, section of a set of students as dictionary type data.

Program
Output
Reading Data from Binary File using load() method: Dictionary Data
Ex-8: Read the dictionary data from the binary file student.dat and print the names of students in class 12.

Program
Output

Ex-9: Define a function called storeItems()


to store a list type data to a binary file
‘items.dat’, where the list stores the item
name, quantity and price of an item. Every
time the function is called, it stores data
regarding one item only. Define another
function called readItems() which reads
the file ‘items.dat’ and prints the name and
quantity of items that are priced more than
1000. The function also return counts the
number of such items and returns the
count value. Call both the functions from
the main part of the code.
WORKING WITH CSV MODULE AND CSV FILES
• CSV (Comma Separated Values) is a simple file format used to store
tabular data, such as a spreadsheet (like Excel) or a database
• A CSV file stores tabular data (both numbers and text) in plain text
• Each line of the file represents a data record
• Each record consists of one or more fields (attributes), separated by
commas (diagram on right showing a sample csv file)
• The use of the comma as a field separator is the source of the name
for this file format. You may also have a different delimiter/separator,
other than a comma, if required
• Python provides a module called the csv module, using which you can work exclusively with CSV Files
• Files that use the csv module are saved with the .csv file extension
• The CSV library provides functionality to both write to and read from a CSV files
o The writerow( ) and writerows() functions of csv module are used to write data to a csv file
o The reader( ) function of csv module is used to read data from a csv file

Working of the with control:


The with statement is used when you
want to execute two related operations
as a pair, with a block of code in-between.
The syntax is:
• Note the syntax of how the file object gets created using the with control
• The above with statement will automatically close the file after the indented block of code is executed.
Here open() and close() are a pair of operations
• It guarantees to close the file no matter how the block of codes under it terminates
• Even in case of an exception before the block ends, the with statement will handle it and close the file

Using a different delimiter (separator) in a csv file


• The reader object can handle different styles of CSV files by specifying additional parameters
• The most common among them is the delimiter parameter
• It specifies the character used to separate each data in the file, if in case the delimiter is not a comma
• However, the default value of the delimiter is the comma (',')
• For example, if a colon character ':' is used as a delimiter in a file, then it has to be specified both during
writing and reading data from the file as:
writerObj = csv.writer ( fobj, delimiter = ':' )
readerObj = csv.reader ( fobj, delimiter = ':' )

Steps to Write Data to a CSV File using writer( ), writerow() and writerows() functions
▪ To write data to a CSV file, we need to first open the CSV file in write mode
▪ The file object then needs to be converted to a csv writer object
▪ Next, the writerow() method can then be used to write a single row into the file
▪ The writerows() method can also be used to write multiple rows at once
Writing Data to a CSV File using writer( ) and writerow() functions
Example-9: The following program is
used to write data to the marks.csv file
using a function called storeMarks().
When the function is called, then
record for a single student is written to
the file. The values stored in each line
of the file are the student’s name,
marks in 3 subjects and the average of
the marks.

As seen from the code, when the function storeMarks() is


called, the ‘marks.csv’ file is opened using the with control.
Next the writer object wObj is created. After that, the data
for a student’s name nm, and the marks in 3 subjects m1,
m2, m3 are input from the user. From the marks, the total
of marks tot is calculated. The list L is formed with the
student data. Here the function is called 4 times from the
main code to store 4 sets of student records.
NOTE that the numeric data
m1, m2, m3, and tot need
not be changed to a string
using str(). The writerow()
function uses this list to
write the data to the file
after converting the numeric data to a string automatically and adding a EOL (‘\n’) character at the end of the line.
Why the newline = '' parameter is used: If the writing operation is done without using the newline parameter,
then if you open the data file created, you will find that after each line an extra blank line gets inserted. The
newline = '' argument prevents the insertion of the extra newlines.
Why the 'a' mode is used: If a file is written within a function, then the mode used should be the append mode.
In case the mode used is write i.e. 'w', then every time you call the function to write data to the file, the previous
content of the file will get erased. But if the file is opened in append mode 'a', then it will preserve the previous
file content and will allow the new data to be added at the end.
In case a function is not used to write data to a file, then the mode used can be 'w', as the file is written fully
outside any functions and the entire data in the file is written together.

Writing Data to a CSV File with a HEADER row, using the writer( ) and writerows() functions
The writerows() method can also be used to write multiple rows at once to a csv file.
Example-10: The following program is used to write multiple lines at ONCE to the employees.csv file. The values
stored in each line are ID, NAME, and SALARY of an employee. A header row is included also at the top.

Program Output
Working of the program:
• Using the with control the employees.csv file is
opened using open() function in write mode and file Header Row
object fout created
• A list called Emps = [ ] is created. This list will store all
the lines to be written to the file as individual lists
Data Rows
• The writer() method is use to convert file object to
csv writer object writerObj
• Next the header list is written to the csv file using the
writerow() method and the writer object csv file created
• Next a for loop is used to input data for n number of
employee data from the user. Note that all the data can be input as both string and numeric data
• Next the input data is used to form a list L = [ID, NM, SL] (it can be a mix of string and numeric values)
• However, this list is not written to the file, but appended to the Emps list
• Finally, outside the loop the writerows() function is used to write the data from the Emps list to the csv file.
Note that the data is NOT stored as a list of lists in the csv file, but the elements from each sub-list L is
extracted, converted to string, and written as a single line of text to the file separated by the delimiter. In
this example, the list Emps looks like:
[ [‘138’, ‘Hritik De’, ‘35000’], [‘157’, ‘Nisha Jain’, ‘40000’], [………….], [……………] ]

Using a different delimiter other than a comma:


Example-11: The following program writes data to the shapes.csv file. The data in each line is separated by a ':'
instead of a comma. The values stored in each line are shape Name, shape Sides, and shape Colour.

Program
Output

csv file created


The working of the program is discussed below:
with delimiter as ':'
• Using the with construct the shapes.csv file is opened using open()
function in write mode and file object fout created
• The writer() method is use to convert file object to csv writer object writerObj. Note that we have used
the delimiter parameter to set the delimiter as a colon ':'
• Next a for loop is used to input data for n number of shapes from the user. Note that all the data are input
as string. You can ONLY write text data to a csv file
• Next the input data is used to form a list L of strings
• Finally the writerow() function is used to write the data from the list to the csv file.
Note that the data is NOT stored as a list in the csv file, but the string elements from the list L are extracted
and written to the file separated by the delimiter
• File is closed at the end
Reading Data from CSV File using reader( ) function
Reading from the employees.csv file
• The employees.csv file is shown on the right. Each individual data in a line
is separated by a comma. The file has a header row also. Each line (other
than header row) stores information about a given student.
• First import the csv module to use functions from csv module
• Using the with construct the employees.csv file is opened using open()
function and file object fin created
• The reader() function reads the entire data in
the file and creates an iterable reader object.
In this example, the reader object is called
readerObj
• Data from each line in the file is stored as a
list of strings in the reader object. Each string
element in the list represents an individual
data from a line in the file. Hence the reader object
stores all the lines from the file as a collection of
lists
• If you print the reader object, it will print the
address of the object. We require a loop to get
the data
Program Output
• As a reader object is an iterable object, data from
readerObj can be extracted using a for loop
• Note from the output shown, how the lines from the file are returned as lists of strings

Example-12: The following program is used to read


data from the marks.csv file using a function called
read Marks(). When the function is called, the
records from the file are read one by one and the
student data for those students where the total of
marks is more than 240.
As seen from the code on the right, when the
function readMarks() is called, the ‘marks.csv’ file is
opened using the with control. Next the reader
object rObj is created.
After that, the for loop can be used to read the lines from the file by directly
using the reader object rObj. Every time the loop runs, one line from the file
is read as a list of strings from the reader object. Hence the variable row
represents a line from the file as a list of strings. For example, for the first
line of the file the list will be like [‘Amit Roy’, ‘85’, ‘82’, ‘88’, ‘255’].
Hence, row[4] indicates the total marks value. As it is
in the form of a string, it is converted to an integer
using the code int(row[4]) and compared with the
value 240. Wherever the condition is True, the row for that student is printed as shown in the output on the right.
The main section of the code
with the function calls is shown
on the right.
Creating a list from the reader object:
If the reader object is printed, though it is an
iterable object, but still only the address of the
reader object will get printed as shown on the right.
Alternatively, the reader object can be converted to a
list using the list() function. In that case we will get a
nested list L, where each sub-list represents a line from
the file as shown below:
The for loop will now read each sub-list and store it in
the loop variable row. The remaining part of the code
works similar to the previous version.

Using the next( ) function to read a single line of data from the iterable reader object
• The next() function can be used to read a single line of record/data from the reader object
• It returns the current row and advances the record pointer to the next row
• In case the first row of a csv file contains the headers (or column headings), we can extract it separately
using the next function and print it
• The next function is used as: row = next ( reader_Object )

Using the line_num parameter to count the number of lines read


• The line_num parameter can be used to view the number of lines read from the csv reader object
• It returns the cumulative count value, i.e. as you go on reading lines from the reader object, it gives the
total number of lines read from the reader object so far
• The line_num parameter is used as: count = reader_Object . line_num
Example-13: The following program reads the data from the employees.csv file and prints the header row
separately. It then prints the data for those employees with salaries more than or equal to 40000.

Program Output

The next() function reads the first line from the file and stores it in the variable header. The remaining lines are
then read using the for loop and the reader object.
Note that the line_num parameter also takes into account the header row in the file and counts it.
Example-14: The following program reads the file shapes.csv that stores a set of shape name, its number of sides
and its colour. However, it uses the ':' as its data separator or delimiter. The program then prints those shape
names which have the colour as red.

Program
Output
csv file with data
separated by ':'

Editing Data Files


General procedure to EDIT Data in a File:
• Open the original file to modify in read mode
• Open a temporary file in write mode
• Using a loop read each line of the original file
o If it is not the record to modify, then write it to the temporary file without any
change as-it-is
o If it is the record to modify, then modify the record and write the modified
record to the temporary file
o Write the remaining records from the original to the temporary file as-it-is
• Close both the files
• Delete the original File
• Rename the temporary file with the original file name

Editing a text file:


The following program
reads a text file called
totalMarks.txt that
stores a list of student
total marks information.
A particular mark is
modified against a roll.
• The os module is
imported to use the
remove (to delete
the original file) and
rename (the
temporary file)
functions
• The original file
totalMarks.txt is
opened in read
mode
• The temporary file is temp.txt opened in write mode
• The Roll of the student whose marks is to be modified is input
• The while loop reads each line of data
• The rstrip() function removes the end of line marker (‘\n’) from the string
• The line is split into a list L with respect
to comma delimiter. In the list
L[0]→roll, L[1]→name, L[2]→class and
L[3]→section, L[4]→total marks
• The L[0] value (i.e. the roll number) is compared with the input Roll.
• If the Roll matches, the new total marks is input to variable tot
• The list components are then joined to form a string rec. Instead of using
L[4], the newly input total marks tot is added
• The modified string rec is written to the temporary file using write()
• If the Roll does not match, then the string read from the original file is
written to the temporary file without modification
• After the entire file is read, both the files are closed
• The original file is deleted using the remove() function
• The temporary file is renamed using the rename() function with the
name of the original file

Editing a binary file:


The program edits the salary
of an employee of the
employee.dat file under the
temp folder of E drive. The
data of each employee is
stored as a dictionary.

A similar concept as the last


program is used to edit a
binary file as shown on the
right. The dictionaries stored
in the file are shown below:
{'EID':1234, 'Name':'Amit Sen',
'Salary': 15000}
{'EID':2589, 'Name':'Nisha Jain',
'Salary': 18000}
{'EID':3678, 'Name':'Abid Hossain',
'Salary': 50000}
{'EID':2587, 'Name':'Riddhi Roy',
'Salary': 40000}

General procedure to DELETE line from a File


• Open the original file to delete in read mode
• Open a temporary file in write mode
• Using a loop read each line of the original file
o If it is not the line to delete, then write it to the temporary file without any change
o If it is the line to be deleted, then do not write the record to the temporary file
o Write the remaining records from the original to the temporary file as-it-is
• Close both the files
• Delete the original File
• Rename the temporary file with the original file name
Example15: The following
program reads a text file called
totalMarks.txt that stores a list of
student total marks and deletes a
given student record against a roll
• The os module is imported to
use the remove (origi nal file)
and rename (temporary file)
functions
• Original file totalMarks.txt
opened in read mode
• Temporary file temp.txt
opened in write mode
• roll of student whose data is to
be deleted is input
• The while loop reads each line
• rstrip() function removes the end of line marker (‘\n’)
• The string read is split at the commas to form a list of strings
• As L[0] represents the roll, it is compared with input roll to delete (roll = 17)
• If the roll does not match, the if condition becomes True and the read data
line is written to the temporary file using the write() function.
• If the condition is False, it means the roll number read matches with the
input roll number. In that case nothing is written to the temporary file
(hence it is effectively removed from temporary file)
• After the entire file is read, both the files are closed
• The original file is deleted using the remove() function
• The temporary file is renamed using the rename() function with the name of the original file
Deleting data from a binary file:
The program deletes the record of an
employee based on the employee ID
from the file employee.dat under the
temp folder of E drive. The data is
stored as a dictionary.
The dictionaries stored in the file are
shown below:
{'EID':1234, 'Name':'Amit Sen', 'Salary': 15000}
{'EID':2589, 'Name':'Nisha Jain', 'Salary':
18000}
{'EID':3678, 'Name':'Abid Hossain', 'Salary':
50000}
{'EID':2587, 'Name':'Riddhi Roy', 'Salary': 40000}

In this code, in case the read employee


ID is not equal to the ID of employee
to be deleted, the record is written to
the temp file. Hence the temporary file contains all the
dictionaries, excepting the dictionary data to be deleted.
General procedure to EDIT/DELETE line from a File WITHOUT using a temp file:
The csv file salary.csv is opened first in
read mode and the entire data is read
using readlines() and stored as a list of
strings in the variable dataList.
The EID of the employee whose record is
to be edited and the new salary of the
employee are both input from the user.
The for loop is then run to access each
string from the dataList. dataList[i]
indicates the ith string i.e. the ith line from
the file. The string is split with respect to
the comma (see data file shown on the
right) to create a list L of the data
components of the line. Here L[0] is the
EID, L[1] is the name, and L[2] is the
salary.
If the EID in L[0] is same as the EID input, then the salary component in
L[2] is replaced by the newSal salary value. The string components of the
list L are then joined with a comma using the join() function. A newline
character ‘\n’ is added at the end of the string created. This new string
newRec is then used to replace the ith data string in the list dataList. Once
the record is modified the control breaks out of the loop to stop the reading process.
The salary.csv file is opened and the modified dataList is written to the file using the writelines() function.
Random Access in Files
• Whenever a read or write operation takes place in a file, the File Pointer moves sequentially in the file
• Every read/write operation takes place from the current position of the file pointer
• There are two functions in Python that allow us to manipulate the File Pointer. These are:
✓ The tell( ) function: It returns the current position of the file pointer in the file
✓ The seek( ) function: It changes the position of the file pointer by placing it at a specified position

Use of tell( ) function with Text file


We will read the text file sentence.txt using the
read( ) method and print the position of the file
pointer using tell( ) function, before every read
operation.
The figure below shows how the file pointer
moves with every read operation on the text
file sentence.txt and byte number on which it
is placed.

Note that the byte number starts from 0 for the


first character in the file.

Example: Consider the text file names.txt. We will read the file line by line and view the position of the file pointer
before every line is read using tell( ) function.

The output of the program is shown below with the file pointer
position displayed:

IMPORTANT: When the file pointer moves in the file, the tell()
function counts the end of line marker '\n' as two different
characters and hence the count
value gets incremented by 2 at
the end of each line.
Use of seek( ) function
The syntax of the seek( ) function is: file_object . seek ( offset , mode )
Offset: It indicates the number of bytes to move. It is an integer value.
Mode: It indicates the reference point from where the file pointer is moved. It has 3 values. These are:
• 0: for moving the file pointer w.r.t. the beginning of the file (default).
For both text and binary files Offset can be 0 or positive
• 1: for moving the file pointer w.r.t. the current position of the file.
Used mainly with binary files.
Offset can be positive, 0, or negative for binary files, but only 0 for text files
• 2: for moving the file pointer w.r.t. the end of the file.
Used mainly with binary files.
Offset can be 0 or negative for binary files, but only 0 for text files.
Note the following:
• You can only move forward within a text file using seek() function
• Backward movement of file pointer not possible from beginning of file (BOF)
• Forward movement of the file pointer is not possible from the end of file (EOF)
• You can move forward and backward within a binary file using seek()
• Backward movement of file pointer not possible from beginning of file (BOF)
• Forward movement of the file pointer is not possible from the end of file (EOF)
Use of seek( ) with Text File & mode 0, 1 and 2
We will use the different modes with the text file sentence.txt to see how the modes work. The code is shown
below. It shows how the file pointer moves with every read operation on the text file sentence.txt and character
number on which it is placed.
Random access to files: using r+ mode
We will use the
different read
modes with the text
file StuData.txt to
see how they work.
The file stores the
ID, Class, Section
and Marks of a set of
students in different lines (see fig. above)
Note: Each line of data is of fixed length

Ex: Program will change the marks for a


given student ID. It first prints the original
file and at the end prints the modified file.
No temp file is used for modification and
both input and output done in same mode.
• When the loop starts, the tell()
function stores in the pos variable the
address of the line to be read.
• After that line is read using the
readline() function, the file pointer
moves to the next line/record. The
pos variable still stores the starting
address of the line that was read, so
that it can be accessed later and changed.
• The read line is split and the ID value compared with the first element L[0] of the list
• If they match, the modified marks is stored in the list element L[3]
• The list elements are joined together using a blank space to form a single string with the list elements
• The file pointer is then moved back to the position stored in pos using the seek() function and the newly
modified string is written to the file overwriting the line to be modified. As the line is of same length as the
original line, it will not affect the other lines in the file and will exactly replace the existing line.

Random access to files: using w+ mode


Example: The next program opens a file in w+ mode and writes employee ID and phone to the file. After every
write it reads back the last record written, and displays it. Finally, it displays the entire final file.
• When the loop starts, the data to store is input from the user.
• The location of the file pointer is stored in the pos variable using the tell() function
• The data string is then written to the file using the write function
• The file pointer is then moved back to the beginning of the last line written to the file at the address stored
in pos using the seek() function.
• The line is then read using the readline() function and displayed. After reading the line, the file pointer places
itself after the line read i.e. the place from where the next line is to be written

You might also like