File Handling
File Handling
File:
A file is a bunch of bytes stored on some storage device
like hard disk.
Data Files:
Data files are the files that store data related to a
specific application for later use.
Data files are of two types.
o Text file
o Binary file
Difference between text file and binary file.
Text File Binary File
A text file stores the Binary file stores the
information in the form of information in the form of
stream of ASCII or stream of bytes.
UNICODE
characters(provide unique
number to every character).
Text file takes time for read There is no translation occur
and write operations because in binary files. So binary files
translation from every are faster and easier for
character to ascii takes place. program read and write than
text files.
In text file, each line is ended In a binary file, there is no
with a special character EOL character.
known as EOL(end of line),
i.e. ‘\n’ or ‘\r\n’
Text files are of two types, Binary file doesn’t have any
1. Regular text file type.
2. Delimited text file.
Or
<file_objectname>=open(<filename>,<mode>)
Ex:
File1=open(“text1.txt”,”r”)
Or
File1=open(“C:\\File\\text1.txt”)
Or
File1=open(r”C:\File\text1.txt”,”r”)
Where,
File1 is a file object.
‘Text1.txt’ is a file opens in read mode and attaches it to
file object ‘File1’.
2. Closing File
After performing operation on file, closing file is important.
In python.
File are automatically closed after end of program, but it is
good practice to close it after performing operation on it.
Syntax:
File_object.close()
File Object
File object or File handle is the reference to a file on disk.
It opens and make it available for different number of task.
It is used to read and write data to a file on disk.
1 r
Opens a file for reading only. The file pointer is placed at the beginning of the
file. This is the default mode.
2
rb
Opens a file for reading only in binary format. The file pointer is placed at the
beginning of the file. This is the default mode.
3
r+
Opens a file for both reading and writing. The file pointer placed at the beginning
of the file.
4
rb+
Opens a file for both reading and writing in binary format. The file pointer placed
at the beginning of the file.
5
w
Opens a file for writing only. Overwrites the file if the file exists. If the file does
not exist, creates a new file for writing.
6
wb
Opens a file for writing only in binary format. Overwrites the file if the file exists.
If the file does not exist, creates a new file for writing.
7
w+
Opens a file for both writing and reading. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
8
wb+
Opens a file for both writing and reading in binary format. Overwrites the existing
file if the file exists. If the file does not exist, creates a new file for reading and
writing.
9
a
Opens a file for appending. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new
file for writing.
10
ab
Opens a file for appending in binary format. The file pointer is at the end of the
file if the file exists. That is, the file is in the append mode. If the file does not
exist, it creates a new file for writing.
11
a+
Opens a file for both appending and reading. The file pointer is at the end of the
file if the file exists. The file opens in the append mode. If the file does not exist,
it creates a new file for reading and writing.
12
ab+
Opens a file for both appending and reading in binary format. The file pointer is
at the end of the file if the file exists. The file opens in the append mode. If the
file does not exist, it creates a new file for reading and writing.
File_object.read([n])
readline() :
Reads a line of the file and returns in form of a string.For
specified n, reads at most n bytes. However, does not reads more
than one line, even if n exceeds the length of the line.
File_object.readline([n])
readlines() :
Reads all the lines and return them as each line a string
element in a list.
File_object.readlines()
Writing to a file
writelines() :
For a list of string elements, each string is inserted in the
text file.Used to insert multiple strings at a single time.
File_object.writelines(L)
( L = [str1, str2, str3] )