SlideShare a Scribd company logo
1 of 34
Download to read offline
File Handling
2
Learning objectives
• Understanding Files
• Data Files
• Types of Files
• File Streams
• Opening and Closing Files
• Reading and Writing Files
3
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.
4
Types of Files
• The data files are the files that store data pretaning to a
specific application, for later use.
• Python allow us to create and manage three types of file
1. TEXT FILE
2. BINARY FILE
3. CSV (Comma separated values) files
5
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.
6
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.
7
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.
8
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
time to process. while reading or writing
operations are performed on file.
No presence of delimiter makes files to
process fast while reading or writing
operations are performed on file.
Text files easy to understand because
these files are in human readable form
Binary files are difficult to understand
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
reflect as soon as the file is opened and
can easily be undone
Can easily get corrupted, even a single bit
change may corrupt the file.
9
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.
Standard Input, Output devices as Files
• If you import sys module in your program then, for reading and writing
for standard I/O devices:
 sys.stdin.read() – Would let you read from Keyboard
 sys.stdout.write()- Would let you write on Monitor
10
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.
11
Python File Open
To open a file for reading it is enough to specify the name of the file:
Syntax
f = open("demofile.txt")
• The code above is the same as:
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.
12
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.
13
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.
14
FILE READING METHODS
• A Program reads a text/binary file from hard disk. File acts like an input
to a program.
• Followings are the methods to read a data from the file.
1. read() METHOD
2. readline() METHOD
3. readlines() METHOD
PYTHON
PROGRAM
15
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()
• Reads only Parts of the File
Syntax:
fileobject.read(size)
• 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",
16
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()
• The open() function returns a file object, which has a readline()
method for reading the content of the file will return only one line
from a file, but demofile.txt file containing three lines of text
Example
f = open("demofile.txt", "r")
print(f.readline())
Returns the first line of the file "demofile.txt",
17
readlines() METHOD
readlines() METHOD
• readlines() method will return a list of strings, each separated by n
• readlines()can be used to read the entire content of the file. You need
to be careful while using it w.r.t. size of memory required before using
the function.
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",
18
FILE WRITING METHODS
• A Program writes into a text/binary file from hard disk.
• Followings are the methods to write a data to the file.
1. write () METHOD
2. writelines() METHOD
• To write to an existing file, you must add a parameter to the open()
function:
 "a" - Append - will append to the end of the file
 "w" - Write - will overwrite any existing content
PYTHON
PROGRAM
19
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()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
Output:
20
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()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
Output:
21
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
22
RANDOM ACCESS METHODS
• All reading and writing functions discussed till now,
work sequentially in the file.
• To access the contents of file randomly –following
methods.
1. seek method
2. tell method
23
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.
24
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.
25
PYTHON FILE OBJECT
ATTRIBUTES
• File attributes give information about the file and file state.
Attribute Function
name Returns the name of the file
closed Returns true if file is closed. False otherwise.
mode The mode in which file is open.
softspace Returns a Boolean that indicates whether a
space character needs to be printed before
another value
when using the print statement.
26
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,
[default])
Iterate over a file when file is used as an iterator, stops
iteration when reaches end-of-file (EOF) for reading.
27
BINARY FILES
CREATING BINARY FILES
SEEING CONTENT OF BINARY FILE
Content of binary file which is in codes.
28
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
29
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)
• A program to write list sequence in a binary file using pickle.dump() method
• Once you try to open list.dat file in python editor to
see the content python generates decoding error.
30
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
31
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
in it starts from the top most directory
Relative Path of file is file location, where
in it starts from the current working directory
32
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
os.path.getsize() show file size in bytes of file passed in parameter
os.path.isfile() Is passed parameter a file
os.path.isdir() Is passed parameter a folder
os.chdir Change directory/folder
os.rename(current,new) Rename a file
os.remove(file_name) Delete a file
33
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'])
34
Thank you
Conclusion!

More Related Content

What's hot (20)

Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
File in C language
File in C languageFile in C language
File in C language
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
File in c
File in cFile in c
File in c
 
Php string function
Php string function Php string function
Php string function
 
Vi editor in linux
Vi editor in linuxVi editor in linux
Vi editor in linux
 

Similar to Python file handling

03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdfbotin17097
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by FaixanٖFaiXy :)
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptxqover
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptRaja Ram Dutta
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdfsulekha24
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleRohitKurdiya1
 
Microsoft power point chapter 5 file edited
Microsoft power point   chapter 5 file editedMicrosoft power point   chapter 5 file edited
Microsoft power point chapter 5 file editedLinga Lgs
 
File handing in C
File handing in CFile handing in C
File handing in Cshrishcg
 

Similar to Python file handling (20)

Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 
Python-files
Python-filesPython-files
Python-files
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
 
Microsoft power point chapter 5 file edited
Microsoft power point   chapter 5 file editedMicrosoft power point   chapter 5 file edited
Microsoft power point chapter 5 file edited
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
File handing in C
File handing in CFile handing in C
File handing in C
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfProf. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaProf. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfProf. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Recently uploaded (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

Python file handling

  • 2. 2 Learning objectives • Understanding Files • Data Files • Types of Files • File Streams • Opening and Closing Files • Reading and Writing Files
  • 3. 3 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.
  • 4. 4 Types of Files • The data files are the files that store data pretaning to a specific application, for later use. • Python allow us to create and manage three types of file 1. TEXT FILE 2. BINARY FILE 3. CSV (Comma separated values) files
  • 5. 5 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.
  • 6. 6 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.
  • 7. 7 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.
  • 8. 8 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 time to process. while reading or writing operations are performed on file. No presence of delimiter makes files to process fast while reading or writing operations are performed on file. Text files easy to understand because these files are in human readable form Binary files are difficult to understand 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 reflect as soon as the file is opened and can easily be undone Can easily get corrupted, even a single bit change may corrupt the file.
  • 9. 9 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. Standard Input, Output devices as Files • If you import sys module in your program then, for reading and writing for standard I/O devices:  sys.stdin.read() – Would let you read from Keyboard  sys.stdout.write()- Would let you write on Monitor
  • 10. 10 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.
  • 11. 11 Python File Open To open a file for reading it is enough to specify the name of the file: Syntax f = open("demofile.txt") • The code above is the same as: 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.
  • 12. 12 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.
  • 13. 13 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.
  • 14. 14 FILE READING METHODS • A Program reads a text/binary file from hard disk. File acts like an input to a program. • Followings are the methods to read a data from the file. 1. read() METHOD 2. readline() METHOD 3. readlines() METHOD PYTHON PROGRAM
  • 15. 15 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() • Reads only Parts of the File Syntax: fileobject.read(size) • 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",
  • 16. 16 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() • The open() function returns a file object, which has a readline() method for reading the content of the file will return only one line from a file, but demofile.txt file containing three lines of text Example f = open("demofile.txt", "r") print(f.readline()) Returns the first line of the file "demofile.txt",
  • 17. 17 readlines() METHOD readlines() METHOD • readlines() method will return a list of strings, each separated by n • readlines()can be used to read the entire content of the file. You need to be careful while using it w.r.t. size of memory required before using the function. 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",
  • 18. 18 FILE WRITING METHODS • A Program writes into a text/binary file from hard disk. • Followings are the methods to write a data to the file. 1. write () METHOD 2. writelines() METHOD • To write to an existing file, you must add a parameter to the open() function:  "a" - Append - will append to the end of the file  "w" - Write - will overwrite any existing content PYTHON PROGRAM
  • 19. 19 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() #open and read the file after the appending: f = open("demofile2.txt", "r") print(f.read()) Output:
  • 20. 20 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() #open and read the file after the appending: f = open("demofile2.txt", "r") print(f.read()) Output:
  • 21. 21 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
  • 22. 22 RANDOM ACCESS METHODS • All reading and writing functions discussed till now, work sequentially in the file. • To access the contents of file randomly –following methods. 1. seek method 2. tell method
  • 23. 23 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.
  • 24. 24 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.
  • 25. 25 PYTHON FILE OBJECT ATTRIBUTES • File attributes give information about the file and file state. Attribute Function name Returns the name of the file closed Returns true if file is closed. False otherwise. mode The mode in which file is open. softspace Returns a Boolean that indicates whether a space character needs to be printed before another value when using the print statement.
  • 26. 26 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, [default]) Iterate over a file when file is used as an iterator, stops iteration when reaches end-of-file (EOF) for reading.
  • 27. 27 BINARY FILES CREATING BINARY FILES SEEING CONTENT OF BINARY FILE Content of binary file which is in codes.
  • 28. 28 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
  • 29. 29 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) • A program to write list sequence in a binary file using pickle.dump() method • Once you try to open list.dat file in python editor to see the content python generates decoding error.
  • 30. 30 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
  • 31. 31 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 in it starts from the top most directory Relative Path of file is file location, where in it starts from the current working directory
  • 32. 32 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 os.path.getsize() show file size in bytes of file passed in parameter os.path.isfile() Is passed parameter a file os.path.isdir() Is passed parameter a folder os.chdir Change directory/folder os.rename(current,new) Rename a file os.remove(file_name) Delete a file
  • 33. 33 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'])