XII Computer Science Gist For Data Files Full
XII Computer Science Gist For Data Files Full
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
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.
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:
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):
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
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.
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.
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.
Program Output
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)
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.
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
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
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
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.
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’], [………….], [……………] ]
Program
Output
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 )
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 ':'
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