Chap.5.File Handling
Chap.5.File Handling
File Handling
The provisions to use and create files through programs are offered by
programming languages.
Data Files: The data files store data pertaining to a specific application.
1. Text Files
2. Binary Files
1. Text Files:
A text file stores information in the form of a stream of ASCII or UNICODE
characters.
In text files, each line of text is terminated (delimited) with a special
character (as per OS) and known as EOL (End Of Line).
In Python, by default, the EOL character is the new line character ‘\n’ or
the carriage return ‘\r’.
e.g.
Regular text file ------------- I am simple text.
TSV file ------------------------ I →am→simple→text.
CSV file ----------------------- I,am,simple,text.
2. Binary Files:
A binary file stores data and information in the form of stream of bytes. It
stores the information in the same format in which the information is held
in memory, i.e. the file returned to you is raw (without any translation).
In binary files there is no delimiter for a line.
The binary files can take variety of extensions. Most non-text file
extensions are binary files.
NOTE: The text files can be opened in any text editor and are in human readable
form while binary files are not in human readable form.
File Object / File Handle – A file object is a reference to a file on disk. It opens
and makes the file available for the different tasks.
File Access Modes – When we open a file in Python then it requires to know the
file mode in which the file is opened. File-mode governs the type of operations
possible in the opened file.
Closing Files: An open file is closed by calling the close() method of its file-
object.
A close() function breaks the link of file-object and the file on the disk. After
close(), no task can be performed on that file through the file-object.
Writing onto text files: Python provides mainly two types of functions to write
onto a data file.
1. write() <file_handle>.write(str1) writes string str1
2. writelines() <file_handle>.writelines(L) writes all strings from
list L
Flush() Function: This function forces the writing of data on disc still pending in
output buffer.
myfile = open("D:\\file\\tax.txt", 'a+')
myfile.write("Hello")
myfile.flush()
myfile.close()
Pickle methods:
1. dump() method – It is used to write in a binary file.
2. load() method - It is used to read from a binary file.
Example:
>>>import pickle
>>>fh = open("D:\\file\\myfile.dat", 'wb')
>>>pickle.dump("Hello", fh)
>>>fh.close()
CSV files are delimited files that store tabular data columns where comma
delimits every value.
The separator character of CSV files is called a delimiter. The common and
most popular delimiter is comma (,). Other popular delimiters are tab (\t),
colon (:), pipe (|) and semi-colon (;) characters.
Python csv module – It provides functionality to read and write tabular data in
CSV format. It provides two specific types of objects – the reader and writer
objects – to read and write into CSV files.