Hanling
Hanling
File Handling
Data File:
The data files are the files that store data pertaining to a specific application, for later use. The data
files can be stored in two ways- Text Files and Binary Files.
Text Files:
Text file stores information in ASCII or Unicode characters, where each line of text is
terminated, (delimited) with a sepcial character known as EOL (End of Line) character. In
text files some internal translations take place when this EOL character is read or written.
Binary Files:
Most of the files that we see in our computer system are called binary files. Example:
Image files: .png, .jpg, .gif etc.
Video files: .mp4, .avi etc.
Audio files: .mp3, .wav etc.
Executable files: .exe, .dll etc.
It can be understood only by a machine or a computer, that’s why they are more secure.
In binary files, there is no delimiter to end a line.
Since they are directly in the form of binary, hence there is no need to translate them.
That’s why these files are easy and fast in working.
File Handling:
In programming, Sometimes, it is not enough to only display the data on the console. Those data
are to be retrieved later on, then the concept of file handling comes. It is impossible to recover the
programmatically generated data again and again. However, if we need to do so, we may store it
on to the file system which is not volatile and can be accessed every time. Here, comes the need of
file handling in Python.
In Python, File Handling consists of following three steps:
1. Opening file.
2. Process file i.e perform read or write operation.
3. Closing file.
1. Opening File:
The open() function is used to open a data file in a program through a file-object (or a
file-handle).
Syntax:
file object=open(<file_name>,<access_mode>)
Example:
obj1=open(“C:\temp\Sample.txt”, “r”)
There are two types of Path : Absolute Path and Relative Path:
Absolute file paths are notated by a leading forward slash or drive label.
For example, /home/example_user/example_directory or C:/system32/cmd.exe. An
absolute file path describes how to access a given file or directory, starting from the
root of the file system. A file path is also called a pathname.
Welcome to Python
It is File Handling.
Reading from Text Files:
Python provide three types of read functions -- read(), readline(), readlines().
a. read( ) reads some bytes from the file and returns it as a string.
Example:
f = open(r"sample.txt", 'r')
text = f.read( )
print(text)
print(“Data returned as - ”,type(text))
f.close( )
Output:
Welcome to Python
It is File Handling.
Data returned as - str
c. readlines( ) reads all the lines from the file and returns it in the form of a list.
Example:
f = open("sample.txt", 'r')
text = f.readlines( )
print(text)
print(“Data returned as - ”,type(text))
f.close()
f = open(r"NewSample.txt", 'a+')
f.write("\n and I am append mode.")
f.close( )
f = open(r"NewSample.txt", 'r')
text = f.read()
print(text)
f.close( )
Output:
Welcome File Handling
I am writing mode.
and I am append mode.
Unpickling: Unpickling refers to the process of converting the byte stream back to the
original structure.
Reading from Binary Files: load( ) function is used to read the data from a file.
Syntax: structure= pickle.load(FileObject)
4. Closing File :
The close() is Used to close an open file. After using this method, an opened file will be
closed and a closed file cannot be read or written any more.
Example:
f = open(r "a.txt", 'a+')
Output:
False
Name of the file is - a.txt
True
Output:
0
Welcom
6
e to Python
It is File Handling.
38
o Python
2. flash( ) function :
The flash( ) function forces the writing of data on disc still pending in output buffer.
Example:
f = open (r”example.txt”,”w+”)
f.write(‘It is an example.’)
f.flush( ) #The string (It is an example.) have been pushed on to actual file on disk