Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views26 pages

File Handling Main

Uploaded by

ydo057696
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

File Handling Main

Uploaded by

ydo057696
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Till now, we were taking the input from the console and writing

it back to the console to interact with the user. Users can easily
handle the files, like read and write the files in Python.

Sometimes, it is not enough to only display the data on the


console. The data to be displayed may be very large, and only a
limited amount of data can be displayed on the console since
the memory is volatile, it is impossible to recover the
programmatically generated data again and again.

The file handling plays an important role when the data needs
to be stored permanently into the file. A file is a named location
on disk to store related information. We can access the stored
information (non-volatile) after the program termination.
DATA FILES

The data files can be stored in two ways:

• Text files

• Binary files
1. Text Files
A text file stores information in the form of a stream of ASCII or
Unicode characters (the one which is default for your
programming platform). In text files, each line of text is
terminated, (delimited) with a special character (as per the
Operating System) known as EOL (End of Line) character. In text
files, some internal translations place when this EOL character
is read or written. In Python, by default, this EOL character is
the newline character ('\n') or carriage-return, newline
combination ('\r\n').
2. Binary Files
A binary file stores the information in the form of a stream of bytes. A binary
file contains information in the same format in which the information is held in
memory, i.e., the file content that is returned to you is raw (with no translation
or no specific encoding). In binary file, there is no delimiter for a line. Also no
translations occur in binary files. As a result, binary files faster and easier for a
program to read and write than are text files. As long as the file doesn't need to
be read by people or need to be ported to a different type of system, binary
files are the best way to store program information.

The binary files can take variety of extensions. Most non-text file extensions
(any application defined extension) are binary files.
OPENING AND CLOSING FILES

The most basic tasks include adding, modifying or deleting data in


a file, which in turn include any one of combination of the
following operations:

• reading data from files

• writing data to files

• appending data to files

Python provides built-in functions to perform each of these tasks.


Path to file
File object created Mode

f = open("c:\\temp\\data.txt", 'r')

As you can see in Fig. 5.1, the slashes in the path are doubled. This
is because the slashes special meaning and meaning escape
sequence for However, if you want to write with single slash, you
may write in raw string as:

f = open(r"c:\temp\data.txt", "r")
File Object/File Handle

File objects are used to read and write data to a file on disk.
The file object is used to obtain a reference to the file on disk
and open it for a number of different tasks.

File object (also called file-handle) is very important and useful


tool as through a file object only, Python program can work
with files stored on hardware. All the functions that you
perform on a data file are performed through file-objects.

When you use file open(), Python stores the reference of


mentioned file in the file-object. A file-object of Python is a
stream of bytes where the data can be read either byte by byte
or line by line or collectively. All this will be clear to you in the
coming lines (under topic - File access modes).
Text File Binary File Mode Description Notes

‘r’ 'rb' read only Default File must exist already, otherwise Python raises I/O
error.
‘w’ 'wb' write only • If the file does not exist, file is created.
• If the file exists, Python will truncate existing data and
over-write in the file. So this mode must be used with
caution.

‘a’ 'ab' append • File is in write only mode.


• If the file exists, the data in the file is retained and new
data being written will be appended to the end.
• If the file does not exist, Python will create a new file.

‘r+’ 'r+b' or'rb+' read and write • File must exist otherwise error is raised.
• Both reading and writing operations can take place.

‘w+’ 'w+b' or'wb+' write and read • File is created if does not exist.
• If file exists, file is truncated (past data is lost).
• Both reading and writing operations can take place.

‘a+’ 'a+b' or'ab+' write and read • File is created if does not exist.
• If file exists, file's existing data is retained; new data is
appended.
• Both reading and writing operations can take place.
A file-mode governs the type of operations (e.g..
read/write/append) possible in the opened file i.e., it refers to
how the file will be used once it's opened.

An open file is closed by calling the close() method of


its file-object. Closing of file is important.

A close() function breaks the link of file-object and the


file on the disk. After close(), no tasks can be
performed on that file through the file-object (or file-
handle).

f.close()
Working with Text Files

Reading from Text Files


read()

<filehandle>.read([n])

reads at most n bytes; if no n is specified, read the entire file.

file1 = open("info.txt", 'r')

readInfo = file1.read(15)
print(readInfo)
type(readInfo)
readline()

<filehandles.readline([n])

reads a line of input; if n is specified reads at most


bytes. Returns the read bytes in the form of a string
ending with In(line) character or returns a blank string
if no more bytes are left for reading in the file.

file1 =open("info.txt", 'r')

readInfo = filel.readline()

print(readInfo)
readlines()

<filehandles.readlines()

reads all lines and returns them in a list

file1 =open("info.txt", 'r')


readInfo =file1.readlines()
print(readInfo)

["It's time to work with files. \n", 'Files offer and ease and power to store your
work/data/information for later use. In', 'simply create a file and store(write) in it
An', 'Or open an existing file and read from it.An']

type(readInfo)
WORKING WITH BINARY FILES

Till now you have learnt to write lines/strings and lists on files. Sometimes you
may need to write and read non-simple objects like dictionaries, tuples, lists or
nested lists and so forth on to the files. Since objects have some structure or
hierarchy associated, it is important that they are stored in way so that their
structure/hierarchy is maintained. For this purpose, objects are often serialized
and then stored in binary files.

◆ Serialisation (also called Pickling) is the process of converting Python object


hierarchy into a byte stream so that it can be written into a file. Pickling converts
an object in byte stream in such a way that it can be reconstructed in original
form when unpickled or de-serialised.

◆ Unpickling is the inverse of Pickling where a byte stream is converted into an


object hierarchy. Unpickling produces the replica of the original object.
"Pickling" is the process whereby a Python
object hierarchy is converted into a byte-
stream, and "unpickling" is the inverse
operation, whereby a byte- stream is
converted back into an object hierarchy.
Python provides the pickle module to achieve this.
import pickle

And then, you may use dump() and load() methods of pickle
module to write and read from an open binary file respectively.
Process of working with binary files is similar to as you have been
doing so far with a little difference that you work with pickle
module in binary files, i.e.,

(1)Import pickle module.


(2)Open binary file in the required file mode (read mode or write
mode).
(3)Process binary file by writing/reading objects using pickle
module's methods.
(4)Once done, close the file.
Writing onto a Binary File

Pickling

In order to write an object on to a binary file opened in the write


mode, you should use dump

function of pickle module as per the following syntax:

pickle.dump(<object-to-be-written>, <file handle-of-open-file>)

For instance, if you have a file open in handle file1 and you want to
write a list namely listl the file, then you may write :

pickle.dump(listi, file1)
import pickle

emp1 = {'Empno': 1201, 'Name': 'Anushree', 'Age' : 25, 'Salary': 47000}


emp2 = {'Empno': 1211, 'Name': 'Zoya', 'Age':30, 'Salary' : 48000}
emp3 = {'Empno': 1251, 'Name': 'Simarjeet', 'Age' : 27, 'Salary': 49000}
emp4 = { 'Empno' : 1266, 'Name': 'Alex', 'Age' : 29, 'Salary': 50000}
empfile = open('Emp.dat', 'wb')
pickle.dump(emp1, empfile)
pickle.dump(emp2, empfile)
pickle.dump(emp3, empfile)
pickle.dump(emp4, empfile)
print("Successfully written four dictionaries")
empfile.close()
import pickle
stu = {}
stufile = open('Stu.dat', 'wb')
ans ='y'
while ans == 'y':
rno = int(input("Enter roll number: "))
name = input("Enter name:")
marks = float(input("Enter marks: "))
stu['Rollno'] = rno
stu['Name'] = name
stu['Marks'] = marks
pickle.dump(stu, stufile)
ans = input("Want to enter more records? (y/n)...")
stufile.close()
import pickle

stu = {}
found = False
fin =open('Stu.dat', 'rb')
Searchkeys= [12, 14]
try:
print("Searching in File Stu.dat...")
while True:
stu = pickle.load(fin)
if stu['Rollno'] in searchkeys:
print(stu)
found = True

except EOFError:
if found == False:
print("No such records found in the file")
else:
print("Search successful.")
fin.close()
import pickle

stu = {}
found = False
print("Searching in file Stu.dat...")
with open('Stu.dat', 'rb') as fin:
stu pickle.load(fin)
if stu[ 'Marks']> 81:
print(stu)
found = True
if found== False:
print("No records with Marks>81)

else:
print("Search successful.")
tell Function
The tell() function returns the current position of file
pointer in the file.

It is used as per the following:

syntax<file-object>.tell()

The seek() Function


The seek() function changes the position of the file-pointer by placing
the file-pointer at the specified position in the open file.
The syntax for using this function is:

<file-object>.seek(offset[, mode])

You might also like