5 File Handling
5 File Handling
5 File Handling
Learning objectives
• Understanding Files
• Data Files
• Types of Files
• File Streams
2
File Handling
• File handling is an important part of any web application.
• A file in itself is a bunch of bytes stored ion some storage
device like hard-disk, thumb-drive etc.,
• Python has several functions for creating, reading, updating,
closing and deleting files.
3
Types of Files
• The data files are the files that store data pretaning to a
specific application, for later use.
2. BINARY FILE
4
TEXT FILE
What is Text File?
• A text file is usually considered as sequence of lines.
• Line is a sequence of characters (ASCII or UNICODE),
stored on permanent storage media.
• The default character coding in python is ASCII each line is
terminated by a special character, known as End of Line
(EOL).
• At the lowest level, text file will be collection of bytes.
• Text files are stored in human readable form and they can
also be created using any text editor.
5
BINARY FILE
What is Binary File?
• A binary file contains arbitrary binary data i.e. numbers
stored in the file, can be used for numerical operation(s).
• So when we work on binary file, we have to interpret the
raw bit pattern(s) read from the file into correct type of data
in our program.
• In the case of binary file it is extremely important that we
interpret the correct data type while reading the file.
• Python provides special module(s) for encoding and
decoding of data for binary file.
6
CSV files
What is CSV File?
• A comma-separated values (CSV) file is a delimited text file
that uses a comma to separate values.
• Each line of the file is a data record.
• Each record consists of one or more fields, separated by
commas.
• The use of the comma as a field separator is the source of
the name for this file format.
• CSV file is used to transfer data from one application to
another.
• CSV file stores data, both numbers and text in a plain text.
7
DIFFERENCE BETWEEN TEXT
FILESAND BINARY FILES
Text Files Binary Files
Text Files are sequential files A Binary file contain arbitrary binary data
Text files only stores texts Binary Files are used to store binary data
such as image, video, audio, text
There is a delimiter EOL (End of Line \n) There is no delimiter
Due to delimiter text files takes more No presence of delimiter makes files to
time to process. while reading or writing process fast while reading or writing
operations are performed on file. operations are performed on file.
Text files easy to understand because Binary files are difficult to understand
these files are in human readable form
Text files are having extension .txt Binary files are having .dat extension
Programming on text files are very easy. Programming on binary files are difficult
Less prone to get corrupt as changes Can easily get corrupted, even a single bit
reflect as soon as the file is opened and change may corrupt the file.
can easily be undone
8
Python File Streams
Standard Input, Output and Error Streams
• There are three different standard streams
Standard input device (stdin) – reads from the keyboard
Standard output device (stdout) – prints to the display and can be
redirected as standard input.
Standard error device (stderr) - Same as stdout but normally only for errors.
9
Python File Open
• The key function for working with files in Python is the open() function.
• The open() function takes two parameters; filename, and mode.
• There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not
exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
• In addition you can specify if the file should be handled as binary or text
mode
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
• These are the default modes. The file pointer is placed at the beginning
for reading purpose, when we open a file in this mode.
10
Python File Open
To open a file for reading it is enough to specify the name of the file:
Syntax
f = open("demofile.txt")
f = open("demofile.txt", "rt")
• Because "r" for read, and "t" for text are the default values, you do not
need to specify them.
• Note: Make sure the file exists, or else you will get an error.
11
FILE ACCESS MODES
• A file mode governs the type of operations like read/write/append
possible methods in the opened file.
MODE Operations on File Opens in
r+ Text File Read & Write Mode
rb+ Binary File Read Write Mode
w Text file write mode
wb Text and Binary File Write Mode
w+ Text File Read and Write Mode
wb+ Text and Binary File Read and Write Mode
a Appends text file at the end of file, a file is created not exists.
ab Appends both text and binary files at the end of file
a+ Text file appending and reading.
ab+ Text and Binary file for appending and reading.
• Example: f=open(“tests.dat”, ‘ab+’)
tests.dat is binary file and is opened in both modes that is reading and
appending.
12
Closing Files
• close()- method will free up all the system resources used
by the file, this means that once file is closed, we will not be
able to use the file object any more.
• fileobject. close() will be used to close the file object, once
we have finished working on it.
• Syntax
<fileHandle>.close()
• For example:
fout.close()
Note: You should always close your files, in some cases, due to buffering,
changes made to a file may not show until you close the file.
13
FILE READING METHODS
• A Program reads a text/binary file from hard disk. File acts like an input
to a program.
PYTHON
PROGRAM
14
read() METHOD
read() METHOD
• By default the read() method returns the whole text, but you can also
specify how many characters you want to return:
• The read() method is used to read entire file
Syntax:
fileobject.read()
• The open() function returns a file object, which has a read() method
for reading the content of the file:
Example
f = open("demofile.txt", "r")
print(f.read(5))
Returns the 5 first characters of the file "demofile.txt",
15
readline() METHOD
readline() METHOD
• readline() will return a line read, as a string from the file. First call to
function will return first line, second call next line and so on.
Syntax:
fileobject.readline()
Example
f = open("demofile.txt", "r")
print(f.readline())
Syntax:
fileobject.readlines()
• As it returns a list, which can then be used for manipulation.
Example
f = open("demofile.txt", "r")
print(f.readlines())
The readlines() method will return a list of strings, each separated by \n
of the file "demofile.txt",
17
FILE WRITING METHODS
• A Program writes into a text/binary file from hard disk.
PYTHON
PROGRAM
18
write () METHOD
• write() method takes a string ( as parameter ) and writes it in the file.
• For storing data with end of line character, you will have to add \n
character to end of the string
• Example
• Open the file "demofile2.txt" and append content to the file:
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
19
write () METHOD
• write() method takes a string ( as parameter ) and writes it in the file.
• if you execute the program n times, the file is opened in w mode
meaning it deletes content of file and writes fresh every time you run.
• Example
• Open the file "demofile3.txt" and overwrite the content:
f = open("demofile2.txt", “w")
f.write("Woops! I have deleted the content!")
f.close()
20
writelines() METHOD
• For writing a string at a time, we use write() method, it can't be used
for writing a list, tuple etc. into a file.
• Sequence data type can be written using writelines() method in the file.
It's not that, we can't write a string using writelines() method.
• So, whenever we have to write a sequence of string / data type, we will
use writelines(), instead of write().
Syntax:
fileobject.writelines(seq)
Example
21
RANDOM ACCESS METHODS
1. seek method
2. tell method
22
RANDOM ACCESS METHODS
Seek() method :
• seek()method can be used to position the file object at
particular place in the file.
syntax is :
fileobject.seek(offset [, from_what])
• Here offset is used to calculate the position of fileobject in
the file in bytes. Offset is added to from_what (reference
point) to get the position.
• Value reference point:
0 -beginning of the file
1 -current position of file
2 -end of file
• Default value of from_what is 0, i.e. beginning of the file.
Example: f.seek(7)
• keeps file pointer at reads the file content from 8th position onwards to till EOF. 23
RANDOM ACCESS METHODS
tell method
• tell() method returns an integer giving the current position of object
in the file.
• The integer returned specifies the number of bytes from the
beginning of the file till the current position of file object.
Syntax:
fileobject.tell()
• tell() method returns an integer and assigned to pos variable. It is
the current position from the beginning of file.
24
PYTHON FILE OBJECT
ATTRIBUTES
• File attributes give information about the file and file state.
Attribute Function
name Returns the name of the file
25
PYTHON FILE OBJECT METHODS
• File methods give information about the file operations/ Manipulation.
Method Function
readable() Returns True/False whether file is readable
writable() Returns True/False whether file is writable
fileno() Return the Integer descriptor used by Python to
request I/O operations from Operating System
flush() Clears the internal buffer for the file.
isatty() Returns True if file is connected to a Tele-TYpewriter
(TTY) device or something similar.
Truncate([size]) Truncate the file, up to specified bytes.
next(iterator, Iterate over a file when file is used as an iterator, stops
[default]) iteration when reaches end-of-file (EOF) for reading.
26
BINARY FILES
CREATING BINARY FILES
27
PICKELING AND UNPICKLING USING
PICKEL MODULE
• Use the python module pickle for structured data such as list or
directory to a file.
• PICKLING refers to the process of converting the structure to a byte
stream before writing to a file.
• while reading the contents of the file, a reverse process called
UNPICKLING is used to convert the byte stream back to the original
structure.
• First we need to import the module, It provides two main methods for
the purpose:-
1) dump() method
2) load() method
28
pickle.dump() Method
• Use pickle.dump() method to write the object in file which is opened in binary
access mode.
Syntax of dump method is:
dump(object,fileobject)
29
pickle.load() Method
• pickle.load() method is used to read the binary file.
• Once you try to open list.dat file in python editor to see the content python
30
HANDLING FILES THROUGH
OS MODULE
• The os module of Python allows you to perform Operating System dependent
operations such as making a folder, listing contents of a folder, know about a
process, end a process etc..
• Let's see some useful os module methods that can help you to handle files and
folders in your program.
ABSOLUTE PATH
RELATIVE PATH
Absolute path of file is file location, where Relative Path of file is file location, where
in it starts from the top most directory in it starts from the current working directory
31
HANDLING FILES THROUGH
OS MODULE
Method Function
os.makedirs() Create a new folder
os.listdir() List the contents of a folder
os.getcwd() Show current working directory
32
CSV File Reading and Writing
• CSV (Comma Separated Values) format is the most common import and
export format for spreadsheets and databases.
• The lack of a well-defined standard means that subtle differences often
exist in the data produced and consumed by different applications.
• These differences can make it annoying to process CSV files from
multiple sources.
• CSV module implements classes to read and write tabular data in CSV
format.
• The CSV module’s reader and writer objects read and write sequences.
csv.reader(csvfile, dialect='excel', **fmtparams)
csv.writer(csvfile, dialect='excel', **fmtparams)
• Programmers can also read and write data in dictionary form using the
DictReader and DictWriter classes.
>>> import csv
>>> with open('names.csv', newline='') as csvfile:
... reader = csv.DictReader(csvfile)
... for row in reader:
... print(row['first_name'], row['last_name'])
33
Conclusion!
Thank you
34