VELAMMAL BODHI DAY SCHOOL
COMPUTER SCIENCE(083)
DATA FILE HANDLING(NOTES)
Files are used to store data permanently and can be retrievedlater.
Type of Files
1. Text Files
2. Binary Files
3. CSV Files
Steps for File handling:
1. Open File
2. Read/Write
3. Close File
Open Files: open( ) function is used to open files in python.
There are two ways to open files in python:
1. file_object = open(“file name”, “ file mode”)
a. i.e. f=open(“test.txt”,”r”) #here our test file exists in the same directory of
python.
b. i.e.
f=open(r”C:\Users\anujd\AppData\Local\Programs\Python\Python311\test.t
xt”,”r”)
Use ‘r’ before path indicates the data within quotes will be read as raw string
and no special meaning attached with any character.
Or
f=open(”C:\\Users\\anujd\\AppData\\Local\\Programs\\Python\\Python311\
\test.txt”,”r”)
The slash in the path has to be doubled.
c. In this we need to close the file.
d. In this mode if we are writing into a file then we have to close the file otherwise
our data will not be written into the file till now the file is not closed. The data
remains in the output buffer and when we close the file then data is shifted
from the output buffer to the file.
e. flush(): It forces the data waiting in the buffer immediately written into the file
without waiting for closing of file.
2. with open(“file name”,”access specifier”) as file_object:
a. i.e. with open(“test.txt”,”r”) as f:
1
b. i.e. with open(r”C:\Users\anujd\AppData\Local\Programs\Python\Python311\test.txt ”,”r”)
as f:
Or
with
open(”C:\\Users\\anujd\\AppData\\Local\\Programs\\Python\\Python311\\t
est.txt”,”r”) as f:
c. In this there is no need to close the file. It will automatically close the file.
Close Files: close( ) function is used to close files in python.
a. file_object.close( )
b. i.e. f.close( )
Access Specifiers/ File modes in Files:
Access Access Access Description File Pointer
Mode Mode Mode for Position
for Text for CSV Files
Files Binary
Files
r rb r Read mode. Opens a file for reading.If Beginning of
the file does not exist, open() raises a File
FileNotFoundError.
r+ rb+ It opens the file for both reading and Beginning of
writing. If the file does not exist, open() File
raises a FileNotFoundError.
w wb w Write mode. It opens the file for writing Beginning of
only. If the file exists, the content of the File
file will be removed. If the file does not
exist, it is created.
w+ wb+ The w+ mode opens the file for both Beginning of
writing and reading. Like w, if the file File
exists, then the content of the file will be
removed. If the file does not exist, it is
created.
2
a Ab a The a mode opens the file for End of File
appending. In this the new content is
added after the existing content. If the
file does not exist, it creates the new file.
a+ ab+ The a+ mode opens the file for both End of File
appending and reading. In this the new
content is added after the existing
content. If the file does not exist, it is
created.
Default mode for file opening in “r” read mode.
By default all files are considered as text files.
If we didn’t specify mode during the openingof the file then it will automatically open the
file in read mode.
File Object Methods (seek( ) & tell( ))
Method Prototype Description
seek( ) Syntax: seek() function is used to change the
<file_object>.seek(<offset>,<from_where>) position of the File Handle to a given
specific position. File handle is like a
where:
cursor, which defines from where
offset: number of positions to be more forward the data has to be read or written in
the file.
from_where: it defines to reference point
Then it returns the new absolute position.
i.e.
0: sets the reference point at the
f.seek(10,0) beginning of the file. (default)
Here, f is the file handle, 10 is the offset (it 1: sets the reference point at the
moves the cursor 10 bytes forward), 0 means current file position.
reference point at the beginning of file.
2: sets the reference point at the end
of the file
Note: In the case of a text file we
can use only ‘0’ as a reference
point.
tell( ) Syntax: <file_object>.tell( ) tell() function returns the current
position of the file object. This
3
I.e. method takes no parameters and
returns an integer value. Initially the
position=f.tell( )
file pointer points to the beginning
Here, position will hold the integer value of the of the file(if not opened in append
file pointer returned by tell function. mode). So, the initial value of tell() is
zero.
f is the file handle.
TEXT FILES:
● It stores information in the form of ASCII or Unicode characters
● Each line of text is terminated with a special character called EOL (End of Line), which
is the new line character (‘\n’) in python by default.
● File extension will be .txt
Working with Text Files:
1. Reading data from a file.
2. Writing data into a file.
Reading data from files:
There are three ways to read data from text file:
1. read function i.e. read( )
2. readline function i.e. readline( )
3. readlines function i.e. readlines( )
read( ) : It is used in text files to read a specified number of data bytes from the file. It returns
the result in the form of a string.
Syntax: file_object.read( )
file_pointer.read(n): It will read the maximum n bytes/characters from the file.
f.read(7) # it will read 7 bytes/characters from the position of file pointer.
file_pointer.read( ): It will read the entire content of the file.
f.read( ) # it will read all the data of the file from the position of file pointer.
readline( ): It will read one complete line in one go from the file. It returns the data in theform of a
string.
Syntax: file_object.readline( )
4
file_pointer.readline( ): It will read the entire line.
f.readline( ) #it will read one complete line in one go.
file_pointer.readline(n): It will read the first ‘n’ bytes from the file.
f.readline(5) #it will read the first 5 characters/bytes from the file.
readlines( ): It will return all the lines of the file as the elements of the list. I.e. the 1st line ofthe file will
be the first element of the list and so on.
Syntax: file_object.readlines( )
file_pointer.readlines( ): It will read all the lines of the file as the elements of the list.
f.readlines( ) #it will read all the lines of the file as the elements of the list.
File Content
Code Output
5
Result is in the form of a list.
6
Writing data into Files:
If the file doesn’t exist then it will create the file.
1. write( ): It takes string as an input and writes it into the file.
a. Syntax: file_object.write(string)
b. i.e. f.write(“Hello World”)
2. writelines( ): It is used to write multiple lines as a list of strings into the file. In this each
element of the list will be treated as a separate line in the file.
a. Syntax: file_object.writelines(list of strings)
b. I.e. data=[“I am a student of DOE”, “I studies in class 12th”]
f.writelines(data)
Code Output
Content in “Topper.txt” file
But, we want these names in separate lines.
7
BINARY FILES:
1. Binary files are made up of non-human readable characters and symbols, which
require specific programs to access its contents.
2. In this translation is not required because data is stored in bytes form.
3. Faster than text files.
4. pickle module is used for working with binary files
a. import pickle
5. File extension will be .dat
6. There is no delimiter to end the file.
Working in Binary files:
Pickle module: pickle module is used in binary file for load( ) and dump( ) methods which are
used for reading and writing into binary file respectively.
Pickling: It is the process of converting python object into byte stream. Pickling is done at the
time of writing into a binary file.
Unpickling: It is the process of converting a byte stream into python object. Unpickling is done
at the time reading from a binary file.
dump( ): it is used to write data into binary file.
Syntax: identifier = pickle.dump(data , file_pointer)
Example: a= “My name is Anuj”
pickle.dump(a,f) #here ‘a’ contains data and ‘f’ is a file pointer.
Program Code Input Details
load( ): it is used to read data from binary file.
8
Syntax: identifier = pickle.load(file_pointer)
Example: data = pickle.load(f) #Here ‘data’ is an identifier and ‘f’ is a file pointer.
9
COMMA SEPARATED VALUES (CSV) FILES:
1. It is a plain text file which stores data in a tabular format, where each row represents
a record and each column represents a field and fields are separated by comma in csv
files.
2. csv module is used for working with csv files
a. import csv
3. File extension for csv file will be .csv
4. CSV module provides two main classes for working with csv files are:
a. reader
b. writer
5. CSV reader object can be used to iterate through the rows of the CSV file.
6. CSV writer object that can be used to write row(s) to the CSV file.
a. writerow(): This method is used to write a single row to the CSV file.
b. writerows(): This method is used to write multiple rows to the CSV file.
7. We can read csv file data in excel file also.
1. Reader Function:
a. For reading data from csv file we require csv. reader( ) function.
2. Writer Function:
a. For writing data to the csv file we take a file object as input and write the data
into the file.
b. writerow( ): It writes a single row of data to the CSV file.
c. writerows( ): It writes a list of rows of data to the CSV file.
WRITING INTO CSV FILES
Code: Output in IDLE:
Result in Notepad Result in Excel:
Code: Output in IDLE:
Result in Notepad: Result in Excel:
Code: Output in IDLE:
Result in Notepad: Result in Excel:
READING FROM CSV FILES:
File Content:
Code: To read all records of csv file Output:
Code: To display all the records in which Output:
name starts with ‘A’
ABSOLUTE AND RELATIVE PATH:
1. Absolute path is a path that starts from the root directory of the file system or we can
say that it describes how to access a given file or directory, from the starting of the file
system.
2. Relative path is a path that is relative to the current working directory or we can say that
is interpreted from the perspective of the current working directory.