SlideShare a Scribd company logo
MODULE 2 – PART 3
FILES
By,
Ravi Kumar B N
Assistant professor, Dept. of CSE
BMSIT & M
INTRODUCTION
✓ Fileisa namedlocationonthe systemstoragewhichrecordsdata forlateraccess.
It enablespersistentstorage ina non-volatilememoryi.e.Harddisk.
✓ It is requiredtoworkwithfilesforeitherwritingtoa fileorreaddatafromit.It is
essentialtostorethe filespermanentlyin secondarystorage.
✓ In python,fileprocessingtakesplaceinthe followingorder.
• Opena filethatreturnsa filehandle.
• Usethe handletoperformreadorwrite action.
• Close thefilehandle.
TYPES OF FILES & OPERATIONS ON FILES
Typesof Files
1. TextFiles- All docfiles/excelfilesetc
2. BinaryFiles- Audiofiles,VideoFiles,imagesetc
OperationsonFile:
▪ Open
▪ Close
▪ Read
▪ Write
✓ Toreadorwritetoa file,you needtoopenitfirst.Toopena fileinPython,use itsbuilt-in
open() function.Thisfunctionreturnsa fileobject,i.e.,a handle.Youcan use ittoread
ormodifythe file.
✓open() filemethod:
file_object= open(“file_name”,” access_mode”)
file_object–Filehandlerthatpointstotheparticularlocationasa referencetoan object
file_name-Name ofthe file
access_mode-Read/write/appendmode. Bydefault,it isset toread-only<r>.
Ex: file1= open("app.log","w")
OPEN A FILE IN PYTHON
✓ Python stores a file in the form of bytes on the disk, so you need to decode them in
strings before reading. And, similarly, encode them while writing texts to the file. This is
done automaticallybyPythonInterpreter.
✓ If the open is successful, the operating system returns us a file handle. The file handle is
not the actual data contained in the file, but instead it is a “handle” that we can use to
read the data. You are given a handle if the requested file exists and you have the
properpermissionstoreadthe file.
✓ If the file does not exist, open will fail with a traceback and you will not get a
handletoaccessthecontents
✓ Afileopening maycauseanerrorduetosomeofthereasonsaslistedbelow
▪ Filemaynotexistinthe specifiedpath(whenwe trytoreada file)
▪ Filemayexist,butwe may nothave a permissiontoread/writea file
▪ Filemight have got corruptedandmay notbe inanopeningstate
Modes Description
r Opens a file only for reading
rb Opens a file only for reading but in a binary format
w Opens a file only for writing; overwrites the file if the file exists
wb Opens a file only for writing but in a binary format
a Opens a file for appending. It does not overwrite the file, just adds the data in the
file, and if file is not created, then it creates a new file
ab Opens a file for appending in a binary format
r+ Opens a file only for reading and writing
w+ Opens a file for both writing and reading. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the
file if the file exists. The file opens in the append mode.
FILE MODES
TEXT FILES & LINES
A text file can be thought of as a sequence oflines, much like a Python stringcan be thought of
as a sequence of characters. For example, this is a sample of a text file which records mail
activityfromvariousindividualsin anopensourceprojectdevelopmentteam:
Tobreakthe fileintolines,thereisa specialcharacterthatrepresentsthe “endofthe line”called
the newlinecharacter.In the textfileeachlinecan beseparatedusingescapecharactern
>>> stuff = 'XnY’
>>> print(stuff)
X
Y
READING THE FILES
When we successfully open a file to read the data from it, the open() function returns the
file handle (or an object reference to file object) which will be pointing to the first
characterinthe file.
Therearedifferentwaysinwhichwe canreadthe filesinpython.
➢ read() //readsallthecontent
➢ read(n) //readsonlythefirstn characters
➢ readline() //readssingleline
➢ readlines() //readsalllines
Where, n isthe numberof bytestobe read
Note: If the file is too large to fit in main memory, you should write your program to read
the fileinchunks usinga fororwhileloop.
SAMPLE PROGRAMS
1.txt
Coronavirus Can Be Stopped Only by Harsh
Steps
Stay at home
wear a facemask
Clean your hands often
Monitor your symptoms
readexample.py
f1=open("1.txt",”r”)
# to read first n bytes
print("---first four
characters---")
print(f1.read(4))
# to read a first line
print("---first line---")
print(f1.readline())
# to read entire file
print("---Entire File---")
print(f1.read())
Output
---first four characters---
Coro
---first line---
navirus Can Be Stopped Only by
Harsh Steps
---Entire File---
Stay at home
wear a facemask
Clean your hands often
Monitor your symptoms
countlines.py
f1 = open(“1.txt”,”r”)
count = 0
for line in f1:
count = count + 1
print('Line Count:', count)
Output
Line Count: 5
Note: When the file is read using a for loop in this manner,
Python takes care of splitting the data in the file into
separate lines using the newline character.
Readlineexample.py
f1=open("1.txt") #by default read mode
#to read line wise
print(f1.readline())
print(f1.readline())
Output
Coronavirus Can Be Stopped Only by Harsh Steps
Stay at home
Countchars.py
#finds the length of the file
f1 = open('1.txt')
ch = f1.read()
print(len(ch))
Output
120
Note:In the above code it counts the
number of characters along with
newline character(n)
WRITING THE FILES:
✓ Towritea dataintoa file,we needtouse themode ‘w’inopen()function.
✓ The write()methodisusedtowritedata intoa file.
>>> fhand=open(“mynewfile.txt","w")
>>> print(fhand)
<_io.TextIOWrapper name='mynewfile.txt' mode='w' encoding='cp1252'>
✓ We have twomethodsforwritingdataintoa fileasshown below
1. write(string)
2. writelines(list)
If the file specified already exists, then the old contents will be erased and it will be ready
to write new data intoit. If the file does not exists, then a new file with the given name will
be created.
write( ) method: It returns number of characters successfully written into a file. The file object
alsokeepstrackofpositionin thefile.
For example,
writelines()method: Example:This code adds the listofcontentsintothe fileincludingn
Writexample.py
fhand=open("2.txt",'w')
s="hello how are you?"
print(fhand.write(s))
Output:
18
Writelist.py
food = ["Citrusn", "Garlicn", "Almondn", "Gingern"]
my_file = open("immunity.txt", "w")
my_file.writelines(food)
Output
It creates a file immunity.txt
Citrus
Garlic
Almond
Ginger
Example for read binary ‘rb’ and write binary ‘wb’
Imagecopy.py
f1=open("bird.jpg",'rb')
f2=open("birdcopy.jpg",'wb')
for i in f1:
print(f2.write(i))
bird.jpg #input file birdcopy.jpg #output file
SEARCHING THROUGH A FILE
✓ When you are searching through data in a file, it is a very common pattern to read
through a file, ignoring most of the lines and only processing lines which meet a
particularcondition.
✓ Mostofthe times,we wouldlike toreada filetosearchforsome specificdata withinit.
Thiscan beachievedbyusingsome stringmethodswhile readinga file.
✓ For example, we may be interested in printing only the line which starts with a
specificcharacter.
SEARCH EXAMPLE
Search1.py
fhand = open('mbox.txt')
for line in fhand:
line = line.rstrip() #strips whitespace from right side of a string
if line.startswith('From:'):
print(line)
Or
fhand = open('mbox-short.txt')
for line in fhand:
line = line.rstrip()
# Skip 'uninteresting lines'
if not line.startswith('From:'):
continue
# Process our 'interesting' line
print(line)
Output
From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
From: louis@media.berkeley.edu
From: zqian@umich.edu Fri Jan 4 16:10:39 2008
mbox.txt
From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
From: louis@media.berkeley.edu
Subject: [sakai] svn commit:
From: zqian@umich.edu Fri Jan 4 16:10:39 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Search2.py
Note:find lines where the search string is anywhere in the
line. Find() method returns either position of a string or
-1
fhand = open('mbox.txt')
for line in fhand:
line = line.rstrip()
if line.find('@uct.ac.za') == -1: continue
print(line)
Output
From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
LETTING THE USER CHOOSE THE FILE NAME
In a real time programming, it is always better to ask the user to enter a name of the file which he/she would
like to open, instead of hard-coding the name of a file inside the program.
Fileuser.py
fname=input("Enter a file name:")
f1=open(fname)
count =0
for line in f1:
count+=1
print("Line Number ",count, ":", line)
print("Total lines=",count)
f1.close()
Output:
Enter a file name:1.txt
Line Number 1 : Coronavirus Can Be Stopped Only by Harsh Steps
Line Number 2 : Stay at home
Line Number 3 : wear a facemask
Line Number 4 : Clean your hands often
Line Number 5 : Monitor your symptoms
Total lines= 5
In this program, the user input filename is received through variable fname, and the same has been used as
an argument to open() method. Now, if the user input is 1.txt (discussed before), then the result would be
Total lines=5
Everything goes well, if the user gives a proper file name as input. But, what if the input filename cannot be
opened (Due to some reason like – file doesn‟t exists, file permission denied etc)?
Obviously, Python throws an error. The programmer need to handle such run- time errors as discussed in the
next section.
USING TRY ,EXCEPT AND OPEN
When you try opening the file which doesn’t exist or if a file name is not valid, then the interpreter throws
you an error. Assume that the open call might fail and add recovery code when the open fails as follows:
In the above program, the command to open a file is kept within try block. If the specified file cannot be
opened due to any reason, then an error message is displayed saying File cannot be opened, and the
program is terminated. If the file could able to open successfully, then we will proceed further to perform
required task using that file.
Tryfile.py
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
exit()
count = 0
for line in fhand:
if line.startswith('From:'):
count = count + 1
print('count=', count)
Output1:
Enter the file name: mbox.txt
count= 3
Output2:
Enter the file name: newmbox.txt
File cannot be opened: newmbox.txt
CLOSE A FILE IN PYTHON
✓ It’s always the best practice to close a file when your work gets finished.
However, Python runs a garbage collector to clean up the unused objects. While
closing a file, the system frees up all resources allocated to it
✓ The most basic way is to call the Python close() method.
Filepointer.close()
Example:
f = open("app.log“)
# do file operations.
f.close()
PROBLEMS WITH WHITE SPACE- REPR()
✓ When we are reading and writing files, we might run into problems with white
space. These errors can be hard to debug because spaces, tabs and newlines are
normally invisible.
>>> s = ‘1 2 t 3 n 4’
>>> print(s)
1 2 3
4
✓ The built in function repr( ) can take any object as an argument and returns a
string representation of the object. For strings, it represents whitespace,
characters with blackslash sequences:
>>> print(s)
‘1 2 t 3 n 4’
✓ This helps for debugging
EXERCISE PROBLEMS:
1) WAP to copy all the lines from one file to another file ( file1.txt to file2.txt) where the line
beginswithvowelsandalso demonstratethecomputationalfaultsintheprogram
2) Write a program to count the number of occurrences of a given word(accept the input
fromuser) ina file.
(hint: Canusestrip()andcount()methodsforeachwordin a line)
3) Input decimal number and convert it to binary number and write it in another file until
userenters0
THANK
YOU

More Related Content

What's hot

File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
File handling-c
File handling-cFile handling-c
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
File Handling in Python
File Handling in PythonFile Handling in Python
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
Edureka!
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
RaginiJain21
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Pandas
PandasPandas
Pandas
maikroeder
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
keeeerty
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 

What's hot (20)

File handling in c
File handling in cFile handling in c
File handling in c
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Python file handling
Python file handlingPython file handling
Python file handling
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Pandas
PandasPandas
Pandas
 
File in C language
File in C languageFile in C language
File in C language
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
List in Python
List in PythonList in Python
List in Python
 

Similar to File handling in Python

Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
RashmiAngane1
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
22261A1201ABDULMUQTA
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
Python-files
Python-filesPython-files
Python-files
Krishna Nanda
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is hereCHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
qover
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
ShaswatSurya
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
Data File Handling in Python Programming
Data File Handling in Python ProgrammingData File Handling in Python Programming
Data File Handling in Python Programming
gurjeetjuneja
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
ARYAN552812
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
4 text file
4 text file4 text file
4 text file
hasan Mohammad
 

Similar to File handling in Python (20)

Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
Python-files
Python-filesPython-files
Python-files
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is hereCHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Data File Handling in Python Programming
Data File Handling in Python ProgrammingData File Handling in Python Programming
Data File Handling in Python Programming
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
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
 
4 text file
4 text file4 text file
4 text file
 

More from BMS Institute of Technology and Management

Software Engineering and Introduction, Activities and ProcessModels
Software Engineering and Introduction, Activities and ProcessModels Software Engineering and Introduction, Activities and ProcessModels
Software Engineering and Introduction, Activities and ProcessModels
BMS Institute of Technology and Management
 
Python Regular Expressions
Python Regular ExpressionsPython Regular Expressions
Python Regular Expressions
BMS Institute of Technology and Management
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho lists
Pytho listsPytho lists
Introduction to the Python
Introduction to the PythonIntroduction to the Python
Introduction to the Python
BMS Institute of Technology and Management
 
15CS562 AI VTU Question paper
15CS562 AI VTU Question paper15CS562 AI VTU Question paper
15CS562 AI VTU Question paper
BMS Institute of Technology and Management
 
weak slot and filler
weak slot and fillerweak slot and filler
strong slot and filler
strong slot and fillerstrong slot and filler
Problems, Problem spaces and Search
Problems, Problem spaces and SearchProblems, Problem spaces and Search
Problems, Problem spaces and Search
BMS Institute of Technology and Management
 
Introduction to Artificial Intelligence and few examples
Introduction to Artificial Intelligence and few examplesIntroduction to Artificial Intelligence and few examples
Introduction to Artificial Intelligence and few examples
BMS Institute of Technology and Management
 

More from BMS Institute of Technology and Management (11)

Software Engineering and Introduction, Activities and ProcessModels
Software Engineering and Introduction, Activities and ProcessModels Software Engineering and Introduction, Activities and ProcessModels
Software Engineering and Introduction, Activities and ProcessModels
 
Python Regular Expressions
Python Regular ExpressionsPython Regular Expressions
Python Regular Expressions
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho dictionaries
 
Pytho lists
Pytho listsPytho lists
Pytho lists
 
Introduction to the Python
Introduction to the PythonIntroduction to the Python
Introduction to the Python
 
15CS562 AI VTU Question paper
15CS562 AI VTU Question paper15CS562 AI VTU Question paper
15CS562 AI VTU Question paper
 
weak slot and filler
weak slot and fillerweak slot and filler
weak slot and filler
 
strong slot and filler
strong slot and fillerstrong slot and filler
strong slot and filler
 
Problems, Problem spaces and Search
Problems, Problem spaces and SearchProblems, Problem spaces and Search
Problems, Problem spaces and Search
 
Introduction to Artificial Intelligence and few examples
Introduction to Artificial Intelligence and few examplesIntroduction to Artificial Intelligence and few examples
Introduction to Artificial Intelligence and few examples
 

Recently uploaded

CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
IJECEIAES
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

File handling in Python

  • 1. MODULE 2 – PART 3 FILES By, Ravi Kumar B N Assistant professor, Dept. of CSE BMSIT & M
  • 2. INTRODUCTION ✓ Fileisa namedlocationonthe systemstoragewhichrecordsdata forlateraccess. It enablespersistentstorage ina non-volatilememoryi.e.Harddisk. ✓ It is requiredtoworkwithfilesforeitherwritingtoa fileorreaddatafromit.It is essentialtostorethe filespermanentlyin secondarystorage. ✓ In python,fileprocessingtakesplaceinthe followingorder. • Opena filethatreturnsa filehandle. • Usethe handletoperformreadorwrite action. • Close thefilehandle.
  • 3. TYPES OF FILES & OPERATIONS ON FILES Typesof Files 1. TextFiles- All docfiles/excelfilesetc 2. BinaryFiles- Audiofiles,VideoFiles,imagesetc OperationsonFile: ▪ Open ▪ Close ▪ Read ▪ Write
  • 4. ✓ Toreadorwritetoa file,you needtoopenitfirst.Toopena fileinPython,use itsbuilt-in open() function.Thisfunctionreturnsa fileobject,i.e.,a handle.Youcan use ittoread ormodifythe file. ✓open() filemethod: file_object= open(“file_name”,” access_mode”) file_object–Filehandlerthatpointstotheparticularlocationasa referencetoan object file_name-Name ofthe file access_mode-Read/write/appendmode. Bydefault,it isset toread-only<r>. Ex: file1= open("app.log","w") OPEN A FILE IN PYTHON
  • 5. ✓ Python stores a file in the form of bytes on the disk, so you need to decode them in strings before reading. And, similarly, encode them while writing texts to the file. This is done automaticallybyPythonInterpreter. ✓ If the open is successful, the operating system returns us a file handle. The file handle is not the actual data contained in the file, but instead it is a “handle” that we can use to read the data. You are given a handle if the requested file exists and you have the properpermissionstoreadthe file.
  • 6. ✓ If the file does not exist, open will fail with a traceback and you will not get a handletoaccessthecontents ✓ Afileopening maycauseanerrorduetosomeofthereasonsaslistedbelow ▪ Filemaynotexistinthe specifiedpath(whenwe trytoreada file) ▪ Filemayexist,butwe may nothave a permissiontoread/writea file ▪ Filemight have got corruptedandmay notbe inanopeningstate
  • 7. Modes Description r Opens a file only for reading rb Opens a file only for reading but in a binary format w Opens a file only for writing; overwrites the file if the file exists wb Opens a file only for writing but in a binary format a Opens a file for appending. It does not overwrite the file, just adds the data in the file, and if file is not created, then it creates a new file ab Opens a file for appending in a binary format r+ Opens a file only for reading and writing w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. FILE MODES
  • 8. TEXT FILES & LINES A text file can be thought of as a sequence oflines, much like a Python stringcan be thought of as a sequence of characters. For example, this is a sample of a text file which records mail activityfromvariousindividualsin anopensourceprojectdevelopmentteam: Tobreakthe fileintolines,thereisa specialcharacterthatrepresentsthe “endofthe line”called the newlinecharacter.In the textfileeachlinecan beseparatedusingescapecharactern >>> stuff = 'XnY’ >>> print(stuff) X Y
  • 9. READING THE FILES When we successfully open a file to read the data from it, the open() function returns the file handle (or an object reference to file object) which will be pointing to the first characterinthe file. Therearedifferentwaysinwhichwe canreadthe filesinpython. ➢ read() //readsallthecontent ➢ read(n) //readsonlythefirstn characters ➢ readline() //readssingleline ➢ readlines() //readsalllines Where, n isthe numberof bytestobe read Note: If the file is too large to fit in main memory, you should write your program to read the fileinchunks usinga fororwhileloop.
  • 10. SAMPLE PROGRAMS 1.txt Coronavirus Can Be Stopped Only by Harsh Steps Stay at home wear a facemask Clean your hands often Monitor your symptoms readexample.py f1=open("1.txt",”r”) # to read first n bytes print("---first four characters---") print(f1.read(4)) # to read a first line print("---first line---") print(f1.readline()) # to read entire file print("---Entire File---") print(f1.read()) Output ---first four characters--- Coro ---first line--- navirus Can Be Stopped Only by Harsh Steps ---Entire File--- Stay at home wear a facemask Clean your hands often Monitor your symptoms countlines.py f1 = open(“1.txt”,”r”) count = 0 for line in f1: count = count + 1 print('Line Count:', count) Output Line Count: 5 Note: When the file is read using a for loop in this manner, Python takes care of splitting the data in the file into separate lines using the newline character. Readlineexample.py f1=open("1.txt") #by default read mode #to read line wise print(f1.readline()) print(f1.readline()) Output Coronavirus Can Be Stopped Only by Harsh Steps Stay at home Countchars.py #finds the length of the file f1 = open('1.txt') ch = f1.read() print(len(ch)) Output 120 Note:In the above code it counts the number of characters along with newline character(n)
  • 11. WRITING THE FILES: ✓ Towritea dataintoa file,we needtouse themode ‘w’inopen()function. ✓ The write()methodisusedtowritedata intoa file. >>> fhand=open(“mynewfile.txt","w") >>> print(fhand) <_io.TextIOWrapper name='mynewfile.txt' mode='w' encoding='cp1252'> ✓ We have twomethodsforwritingdataintoa fileasshown below 1. write(string) 2. writelines(list) If the file specified already exists, then the old contents will be erased and it will be ready to write new data intoit. If the file does not exists, then a new file with the given name will be created.
  • 12. write( ) method: It returns number of characters successfully written into a file. The file object alsokeepstrackofpositionin thefile. For example, writelines()method: Example:This code adds the listofcontentsintothe fileincludingn Writexample.py fhand=open("2.txt",'w') s="hello how are you?" print(fhand.write(s)) Output: 18 Writelist.py food = ["Citrusn", "Garlicn", "Almondn", "Gingern"] my_file = open("immunity.txt", "w") my_file.writelines(food) Output It creates a file immunity.txt Citrus Garlic Almond Ginger
  • 13. Example for read binary ‘rb’ and write binary ‘wb’ Imagecopy.py f1=open("bird.jpg",'rb') f2=open("birdcopy.jpg",'wb') for i in f1: print(f2.write(i)) bird.jpg #input file birdcopy.jpg #output file
  • 14. SEARCHING THROUGH A FILE ✓ When you are searching through data in a file, it is a very common pattern to read through a file, ignoring most of the lines and only processing lines which meet a particularcondition. ✓ Mostofthe times,we wouldlike toreada filetosearchforsome specificdata withinit. Thiscan beachievedbyusingsome stringmethodswhile readinga file. ✓ For example, we may be interested in printing only the line which starts with a specificcharacter.
  • 15. SEARCH EXAMPLE Search1.py fhand = open('mbox.txt') for line in fhand: line = line.rstrip() #strips whitespace from right side of a string if line.startswith('From:'): print(line) Or fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() # Skip 'uninteresting lines' if not line.startswith('From:'): continue # Process our 'interesting' line print(line) Output From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 From: louis@media.berkeley.edu From: zqian@umich.edu Fri Jan 4 16:10:39 2008 mbox.txt From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 Return-Path: <postmaster@collab.sakaiproject.org> From: louis@media.berkeley.edu Subject: [sakai] svn commit: From: zqian@umich.edu Fri Jan 4 16:10:39 2008 Return-Path: <postmaster@collab.sakaiproject.org> Search2.py Note:find lines where the search string is anywhere in the line. Find() method returns either position of a string or -1 fhand = open('mbox.txt') for line in fhand: line = line.rstrip() if line.find('@uct.ac.za') == -1: continue print(line) Output From: stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  • 16. LETTING THE USER CHOOSE THE FILE NAME In a real time programming, it is always better to ask the user to enter a name of the file which he/she would like to open, instead of hard-coding the name of a file inside the program. Fileuser.py fname=input("Enter a file name:") f1=open(fname) count =0 for line in f1: count+=1 print("Line Number ",count, ":", line) print("Total lines=",count) f1.close() Output: Enter a file name:1.txt Line Number 1 : Coronavirus Can Be Stopped Only by Harsh Steps Line Number 2 : Stay at home Line Number 3 : wear a facemask Line Number 4 : Clean your hands often Line Number 5 : Monitor your symptoms Total lines= 5 In this program, the user input filename is received through variable fname, and the same has been used as an argument to open() method. Now, if the user input is 1.txt (discussed before), then the result would be Total lines=5 Everything goes well, if the user gives a proper file name as input. But, what if the input filename cannot be opened (Due to some reason like – file doesn‟t exists, file permission denied etc)? Obviously, Python throws an error. The programmer need to handle such run- time errors as discussed in the next section.
  • 17. USING TRY ,EXCEPT AND OPEN When you try opening the file which doesn’t exist or if a file name is not valid, then the interpreter throws you an error. Assume that the open call might fail and add recovery code when the open fails as follows: In the above program, the command to open a file is kept within try block. If the specified file cannot be opened due to any reason, then an error message is displayed saying File cannot be opened, and the program is terminated. If the file could able to open successfully, then we will proceed further to perform required task using that file. Tryfile.py fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() count = 0 for line in fhand: if line.startswith('From:'): count = count + 1 print('count=', count) Output1: Enter the file name: mbox.txt count= 3 Output2: Enter the file name: newmbox.txt File cannot be opened: newmbox.txt
  • 18. CLOSE A FILE IN PYTHON ✓ It’s always the best practice to close a file when your work gets finished. However, Python runs a garbage collector to clean up the unused objects. While closing a file, the system frees up all resources allocated to it ✓ The most basic way is to call the Python close() method. Filepointer.close() Example: f = open("app.log“) # do file operations. f.close()
  • 19. PROBLEMS WITH WHITE SPACE- REPR() ✓ When we are reading and writing files, we might run into problems with white space. These errors can be hard to debug because spaces, tabs and newlines are normally invisible. >>> s = ‘1 2 t 3 n 4’ >>> print(s) 1 2 3 4 ✓ The built in function repr( ) can take any object as an argument and returns a string representation of the object. For strings, it represents whitespace, characters with blackslash sequences: >>> print(s) ‘1 2 t 3 n 4’ ✓ This helps for debugging
  • 20. EXERCISE PROBLEMS: 1) WAP to copy all the lines from one file to another file ( file1.txt to file2.txt) where the line beginswithvowelsandalso demonstratethecomputationalfaultsintheprogram 2) Write a program to count the number of occurrences of a given word(accept the input fromuser) ina file. (hint: Canusestrip()andcount()methodsforeachwordin a line) 3) Input decimal number and convert it to binary number and write it in another file until userenters0