File Handling Notes
File Handling Notes
I. INTRODUCTION:
• 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).
– Open a file
1) Binary file
2) Text file
• A text file is usually considered as sequence of lines. Line is a sequence of characters (ASCII), stored on permanent
storage media.
• In text file, each line is terminated by a special character, known as End of Line (EOL). From strings we know that \n
is newline character.
• At the lowest level, text file is collection of bytes. Text files are stored in human readable form. They can also be
created using any text editor.
• Text files don’t have any specific encoding and it can be opened in normal text editor itself.
• The method open() is used to open an existing file or creating a new file. If the complete directory is not given then
the file will be created in the directory in which the python file is stored.
<file_object name>= open( <file_name>, <access mode>)
<file_object name>= open( r <file_name>, <access mode>)
The prefix r makes it a raw string specifies no special meaning attached to any character
ACCESS MODES
Default mode for file opening in “r” read mode. If we didn’t specify mode during the
openingof the file then it will automatically open the file in read mode.
w+ wb+ The w+ mode opens the file for both writing and Beginning of
reading. Like w, if the file exists, then the content of the File
file will be removed. If the file does not exist, it is
created.
a ab a The a mode opens the file forappending. In this the End of File
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 appending and End of File
reading. In this the new content is added after the
existingcontent. If the file does not exist, it is created.
• After processing the content in a file, the file must be saved and closed.
<file_object>.close()
READING FILES IN PYTHON
• In order to read a file in python, we must open the file in read mode.
• There are three ways in which we can read the files in python.
• All these read function also read leading and trailing whitespaces, new line characters. If we want to remove these
characters you can use functions
• In order to write data into a file, we must open the file in write mode.
• We need to be very careful while writing data into the file as it overwrites the content present inside the file that
we are writing, and all the previous data will be erased.
• We have two methods for writing data into a file as shown below.
2. writelines( ): It is used to write multiple lines as a list of strings into the file. In
this eachelement of the list will be treated as a separate line in the file.
a. Syntax: file_object.writelines(list of strings)
b. data=[“I am a student of DOE”, “I studies in class 12th”]
f.writelines(data)
FILE POINTER
• Every file maintains a file pointer which tells the current position in the file where reading and writing operation
will take.
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.
Example:
Example:
data = pickle.load(f) #Here ‘data’ is an identifier and ‘f’ is a file pointer.
COMMA SEPARATED VALUE (CSV) FILES:
• 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.
• File extension for csv file will be .csv
Reader Function:
For reading data from csv file we require csv. reader( ) function.
Writer Function:
For writing data to the csv file we take a file object as input and write the data into the file.
writerow( ): It writes a single row of data to the CSV file.
writerows( ): It writes a list of rows of data to the CSV file.