File I&O
File I&O
Learning objectives
• Understanding Files
• Data Files
• Types of Files
• File Streams
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.
Types of Files
• The data files are the files that store data pretaning to a
specific application, for later use.
2. BINARY FILE
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.
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.
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.
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
reflect as soon as the file is opened bit change may corrupt the file.
and 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()
Output:
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
25
in it starts from the top most directoryRelative Path of file is file location, where
in it starts from the current working directory
26
Conclusion!Thank
you
27