SlideShare a Scribd company logo
1 of 12
Name of the Teacher: Dr. Madhavi Vaidya Class:FYBSC Subject: Computer Sc Paper: Python II
Objectives of File Handling
• File
• Working with open()
• read()
• open()
• write()
• readline()
• Close()
File
• Python too supports file handling(like other programming languages) and
allows users to handle files i.e., to read and write files, along with many other
file handling options, to operate on files.
• The concept of file handling has stretched over various other languages, but
the implementation is either complicated or lengthy, but alike other concepts
of Python, this concept here is also easy and short.
• Python treats file differently as text or binary and this is important.
• Each line of code includes a sequence of characters and they form text file
Hence, a file operation can be done in the following order.
•Open a file
•Read or write - Performing operation
•Close the file
Working with open()
We use open () function in Python to open a file in read or write mode. As explained
above, open ( ) will return a file object.
To return a file object we use open() function along with two arguments, that accepts
file name and the mode, whether to read or write.
So, the syntax being: open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
•“ r “, for reading.
•“ w “, for writing.
•“ a “, for appending.
•“ r+ “, for both reading and writing
•“x" - Create - Creates the specified file, returns an error if the file exists
•"t" - Text - Default value. Text mode
•"b" - Binary - Binary mode (e.g. images)
• Refer file3new.py
Open()
• One must keep in mind that the mode argument is
not mandatory. If not passed, then Python will
assume it to be “ r ” by default.
• The open command will open the file in the read
mode and the for loop will print each line present in
the file.
• Refer file3new.py
“rt” mode/no mode/”r” mode
• To open a file for reading it is enough to specify the name of the file:
• f = open("demofile.txt") [by x/w parameter we can create a new file too if not
present]
• The code above is the same as below:
• f = open("demofile.txt", "rt")
• f = open("demofile.txt", "r")
read()
• Reading the contents of the file
• The open() function returns a file object, which has a read() method for reading the
content of the file:-
f = open("demofile.txt", "r")
print(f.read())
• Read Only Parts of the File(in bytes)
By default the read() method returns the whole text, but you can also specify how
many characters you want to return:
Read() can be used with/without parameters
f = open("demofile.txt", "r")
print(f.read(5))
-------------------------------------------
Next slide….read(),readline(),readlines()
Use file1.py – from PD/file
read(),readline(),readlines()
f=open("foo.txt","r")
print(f.read()) #all lines would be read
#print(f.read(4)) #it would read 4 bytes
#print(f.readline(3)) #would read char by char ahead
print(f.readlines()) # all lines would be read with n delimiter
1.Read ( ): Returns the read bytes in the form of a string. ...
2.Readline ( ): Reads a line of the file and returns in the form of a
string (if parameter passed then that many bytes, else first line
3.readlines ( ): Reads all the lines and returns them as a string
element in a list with a new line char after every line
Creating a file using write() mode
• Using write() mode
To manipulate the file, write the following in
your Python environment:
# Python code to create a file
file = open('geek.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular
file")
file.close()
• Refer file7.py
• Working of append() mode
Python code to illustrate append mode
file = open('geek.txt’,'a')
file.write("This will add this line")
file.close()
file = open("foo.txt","w")
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file = open("foo.txt","r") #if we dont write this, the file can’t be read
print(file.read())
file.close()
------------------------------
#Python program to append contents of one file to another file
name1=input("enter the file to be read from:")
name2=input("enter the file to be appended into")
in1=open(name1,"r")
data=in1.read()
in1.close()
out=open(name2,"a")
out.write(data)
out.close()
Read Lines
• You can return one line by using the readline() method:
• Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
By calling readline() two times, you can read the two first lines:
Example
Read two lines of the file:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
Looping thru the file and close()
• By looping through the lines of the file, you can read
the whole file, line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
• Closing the file –close()
f = open("demofile.txt", "r")
print(f.readline())
f.close()
Writing to an Existing file
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
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())
Writing to the file by “w” mode
# open the file.txt in append mode. Create a new file if no such file exists.
fileptr = open("file2.txt", "w")
# appending the content to the file
fileptr.write('''''Python is the modern day language. It makes things so simp
le,It is the fastest-growing programing language''')
# closing the opened the file
fileptr.close()

More Related Content

Similar to File Handling Topic for tech management you know na tho kyuon puch raha hai sale

Similar to File Handling Topic for tech management you know na tho kyuon puch raha hai sale (20)

03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
files.pptx
files.pptxfiles.pptx
files.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
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
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 as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
 
FILE HANDLING.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 
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 in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python - Lecture 8
Python - Lecture 8Python - Lecture 8
Python - Lecture 8
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
File handing in C
File handing in CFile handing in C
File handing in C
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

File Handling Topic for tech management you know na tho kyuon puch raha hai sale

  • 1. Name of the Teacher: Dr. Madhavi Vaidya Class:FYBSC Subject: Computer Sc Paper: Python II Objectives of File Handling • File • Working with open() • read() • open() • write() • readline() • Close()
  • 2. File • Python too supports file handling(like other programming languages) and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. • The concept of file handling has stretched over various other languages, but the implementation is either complicated or lengthy, but alike other concepts of Python, this concept here is also easy and short. • Python treats file differently as text or binary and this is important. • Each line of code includes a sequence of characters and they form text file Hence, a file operation can be done in the following order. •Open a file •Read or write - Performing operation •Close the file
  • 3. Working with open() We use open () function in Python to open a file in read or write mode. As explained above, open ( ) will return a file object. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write. So, the syntax being: open(filename, mode). There are three kinds of mode, that Python provides and how files can be opened: •“ r “, for reading. •“ w “, for writing. •“ a “, for appending. •“ r+ “, for both reading and writing •“x" - Create - Creates the specified file, returns an error if the file exists •"t" - Text - Default value. Text mode •"b" - Binary - Binary mode (e.g. images) • Refer file3new.py
  • 4. Open() • One must keep in mind that the mode argument is not mandatory. If not passed, then Python will assume it to be “ r ” by default. • The open command will open the file in the read mode and the for loop will print each line present in the file. • Refer file3new.py
  • 5. “rt” mode/no mode/”r” mode • To open a file for reading it is enough to specify the name of the file: • f = open("demofile.txt") [by x/w parameter we can create a new file too if not present] • The code above is the same as below: • f = open("demofile.txt", "rt") • f = open("demofile.txt", "r")
  • 6. read() • Reading the contents of the file • The open() function returns a file object, which has a read() method for reading the content of the file:- f = open("demofile.txt", "r") print(f.read()) • Read Only Parts of the File(in bytes) By default the read() method returns the whole text, but you can also specify how many characters you want to return: Read() can be used with/without parameters f = open("demofile.txt", "r") print(f.read(5)) ------------------------------------------- Next slide….read(),readline(),readlines() Use file1.py – from PD/file
  • 7. read(),readline(),readlines() f=open("foo.txt","r") print(f.read()) #all lines would be read #print(f.read(4)) #it would read 4 bytes #print(f.readline(3)) #would read char by char ahead print(f.readlines()) # all lines would be read with n delimiter 1.Read ( ): Returns the read bytes in the form of a string. ... 2.Readline ( ): Reads a line of the file and returns in the form of a string (if parameter passed then that many bytes, else first line 3.readlines ( ): Reads all the lines and returns them as a string element in a list with a new line char after every line
  • 8. Creating a file using write() mode • Using write() mode To manipulate the file, write the following in your Python environment: # Python code to create a file file = open('geek.txt','w') file.write("This is the write command") file.write("It allows us to write in a particular file") file.close() • Refer file7.py • Working of append() mode Python code to illustrate append mode file = open('geek.txt’,'a') file.write("This will add this line") file.close() file = open("foo.txt","w") file.write("This is the write command") file.write("It allows us to write in a particular file") file = open("foo.txt","r") #if we dont write this, the file can’t be read print(file.read()) file.close() ------------------------------ #Python program to append contents of one file to another file name1=input("enter the file to be read from:") name2=input("enter the file to be appended into") in1=open(name1,"r") data=in1.read() in1.close() out=open(name2,"a") out.write(data) out.close()
  • 9. Read Lines • You can return one line by using the readline() method: • Read one line of the file: f = open("demofile.txt", "r") print(f.readline()) By calling readline() two times, you can read the two first lines: Example Read two lines of the file: f = open("demofile.txt", "r") print(f.readline()) print(f.readline())
  • 10. Looping thru the file and close() • By looping through the lines of the file, you can read the whole file, line by line: f = open("demofile.txt", "r") for x in f: print(x) • Closing the file –close() f = open("demofile.txt", "r") print(f.readline()) f.close()
  • 11. Writing to an Existing file 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 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())
  • 12. Writing to the file by “w” mode # open the file.txt in append mode. Create a new file if no such file exists. fileptr = open("file2.txt", "w") # appending the content to the file fileptr.write('''''Python is the modern day language. It makes things so simp le,It is the fastest-growing programing language''') # closing the opened the file fileptr.close()