SlideShare a Scribd company logo
Python Certification Training https://www.edureka.co/python
Agenda
File Handling In Python
Python Certification Training https://www.edureka.co/python
Agenda
File Handling In Python
Python Certification Training https://www.edureka.co/python
Agenda
Introduction 01
Why need File Handling?
Getting Started 02
Concepts 03
Practical Approach 04
Types of Files
Looking at code to
understand theory
Python File Handling System
Python Certification Training https://www.edureka.co/python
Why need File Handling?
File Handling In Python
Python Certification Training https://www.edureka.co/python
Why Need File Handling?
Python Input
Arguments Standard Input
Files
Python Certification Training https://www.edureka.co/python
Types of Files
File Handling In Python
Python Certification Training https://www.edureka.co/python
Types Of Files
What you may know as a file is slightly different in Python
Text
BinaryImage Text
AudioExecutable
Python Certification Training https://www.edureka.co/python
What is File Handling?
File Handling In Python
Python Certification Training https://www.edureka.co/python
What Is File Handling?
File handling is an important part of any web application
Operations
Creating DeletionReading Updating
CRUD
Python Certification Training https://www.edureka.co/python
Python File Handling System
File Handling In Python
Python Certification Training https://www.edureka.co/python
Python File Handling System
The key function for working with files in Python is the open() function
open()
Filename Mode
Syntax open( filename, mode)
WORK
Create File
Open File
WORK
Close File
Python Certification Training https://www.edureka.co/python
Python File Handling System
The key function for working with files in Python is the open() function
Syntax open( filename, mode)
Any name that you want
Different 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
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
In addition you can specify if the file should be handled as binary or text mode
Python Certification Training https://www.edureka.co/python
Python File Handling System
Example Code
Example f = open(ā€œdemofile.txtā€)
Example f = open(ā€œdemofile.txtā€, ā€œrā€)
Note: Make sure file exists or else error!
Python Certification Training https://www.edureka.co/python
File Operations for Reading
File Handling In Python
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.read()
Lots of ways to read a text file in Python
All characters Some characters
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.read()
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.read(5) This 5 indicates what?
Python Certification Training https://www.edureka.co/python
Reading Text File In Python
file.read()
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.readline(): Line by line output
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.readline(3): Read third line only
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> print file.readlines(): Read lines separately
Python Certification Training https://www.edureka.co/python
Looping Over A File Object
Fast and efficient!
Example
> file = open(ā€œtestfile.textā€, ā€œrā€)
> for line in file:
> print file.readline():
Looping over the object
Reading from files
Python Certification Training https://www.edureka.co/python
Python File Write Method
File Handling In Python
Python Certification Training https://www.edureka.co/python
File Write Method
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
Example
> f = open("demofile.txt", "a")
> f.write(ā€œ We love Edureka!")
Example
> f = open("demofile.txt", ā€œw")
> f.write(ā€œ We love Edureka!")
Note: the "w" method will
overwrite the entire file.
Python Certification Training https://www.edureka.co/python
File Write Method
Example
> file = open(ā€œtestfile.txtā€, ā€œwā€)
> file.write(ā€œThis is a testā€)
> file.write(ā€œTo add more lines.ā€)
> file.close()
I’m writing files!
Python Certification Training https://www.edureka.co/python
Creating a New File
File Handling In Python
Python Certification Training https://www.edureka.co/python
Creating A New File
open() method again
āž¢ file = open(ā€œtestfile.txtā€, ā€œxā€)
āž¢ file = open(ā€œtestfile.txtā€, ā€œwā€)
To create a new file in Python, use the open() method, with one of the following parameters:
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
Python Certification Training https://www.edureka.co/python
Deletion Operations
File Handling In Python
Python Certification Training https://www.edureka.co/python
Deleting A File
os.remove() function
To delete a file, you must import the OS module, and run its os.remove() function:
Example
> import os
> os.remove("demofile.txt")
Deleting a folder?
Example
> import os
> os.rmdir("myfolder")
Check if file exists
> import os
> if os.path.exists("demofile.txt"):
> os.remove("demofile.txt")
> else:
> print("The file does not exist")
Python Certification Training https://www.edureka.co/python
Conclusion
File Handling In Python
Copyright Ā© 2019, edureka and/or its affiliates. All rights reserved.
Python File Handling | File Operations in Python | Learn python programming | Edureka

More Related Content

PPTX
Internet of Things, TYBSC IT, Semester 5, Unit IV
Arti Parab Academics
Ā 
PPTX
AWS PPT.pptx
GauravSharma164138
Ā 
PPTX
File handling in Python
Megha V
Ā 
PPTX
ARTIFICIAL INTELLIGENCE BASIC PPT
RohitYemul1
Ā 
PPTX
Counting Sort
Faiza Saleem
Ā 
PPTX
What Is Data Science? Data Science Course - Data Science Tutorial For Beginne...
Edureka!
Ā 
PDF
Hadoop Overview & Architecture
EMC
Ā 
PPTX
HADOOP TECHNOLOGY ppt
sravya raju
Ā 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Arti Parab Academics
Ā 
AWS PPT.pptx
GauravSharma164138
Ā 
File handling in Python
Megha V
Ā 
ARTIFICIAL INTELLIGENCE BASIC PPT
RohitYemul1
Ā 
Counting Sort
Faiza Saleem
Ā 
What Is Data Science? Data Science Course - Data Science Tutorial For Beginne...
Edureka!
Ā 
Hadoop Overview & Architecture
EMC
Ā 
HADOOP TECHNOLOGY ppt
sravya raju
Ā 

What's hot (20)

PPTX
Python: Modules and Packages
Damian T. Gordon
Ā 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
Ā 
PDF
File handling in Python
BMS Institute of Technology and Management
Ā 
PDF
Python libraries
Prof. Dr. K. Adisesha
Ā 
PDF
Object oriented approach in python programming
Srinivas Narasegouda
Ā 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
Ā 
PDF
Python exception handling
Mohammed Sikander
Ā 
PPT
Java Input Output and File Handling
Sunil OS
Ā 
PPTX
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
Ā 
PPT
Python GUI Programming
RTS Tech
Ā 
PPTX
Introduction to Basics of Python
Elewayte
Ā 
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
Ā 
PPTX
Packages In Python Tutorial
Simplilearn
Ā 
PPT
Python ppt
Mohita Pandey
Ā 
PPTX
Datastructures in python
hydpy
Ā 
PPT
Introduction To Django
Jay Graves
Ā 
ODP
Python Modules
Nitin Reddy Katkam
Ā 
PPTX
Python Scipy Numpy
Girish Khanzode
Ā 
PPTX
Looping statement in python
RaginiJain21
Ā 
Python: Modules and Packages
Damian T. Gordon
Ā 
Chapter 03 python libraries
Praveen M Jigajinni
Ā 
Python libraries
Prof. Dr. K. Adisesha
Ā 
Object oriented approach in python programming
Srinivas Narasegouda
Ā 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
Ā 
Python exception handling
Mohammed Sikander
Ā 
Java Input Output and File Handling
Sunil OS
Ā 
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
Ā 
Python GUI Programming
RTS Tech
Ā 
Introduction to Basics of Python
Elewayte
Ā 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
Ā 
Packages In Python Tutorial
Simplilearn
Ā 
Python ppt
Mohita Pandey
Ā 
Datastructures in python
hydpy
Ā 
Introduction To Django
Jay Graves
Ā 
Python Modules
Nitin Reddy Katkam
Ā 
Python Scipy Numpy
Girish Khanzode
Ā 
Looping statement in python
RaginiJain21
Ā 
Ad

Similar to Python File Handling | File Operations in Python | Learn python programming | Edureka (20)

PPTX
Python Session - 5
AnirudhaGaikwad4
Ā 
PDF
Python_AdvancedUnit - 3.pdf about the python
gpsign134
Ā 
PDF
File handling4.pdf
sulekha24
Ā 
PPTX
UNIT –5.pptxpython for engineering students
SabarigiriVason
Ā 
PPTX
File handling for reference class 12.pptx
PreeTVithule1
Ā 
PPTX
Files in Python.pptx
Koteswari Kasireddy
Ā 
PPTX
Files in Python.pptx
Koteswari Kasireddy
Ā 
PDF
File handling3.pdf
nishant874609
Ā 
PDF
Web Development Course: PHP lecture 3
Gheyath M. Othman
Ā 
PPTX
pspp-rsk.pptx
ARYAN552812
Ā 
PPTX
Python File Handling52616416416 ppt.pptx
saiwww3841k
Ā 
PDF
Python-files
Krishna Nanda
Ā 
DOCX
python file handling
jhona2z
Ā 
PPT
File Handling as 08032021 (1).ppt
Raja Ram Dutta
Ā 
PDF
Module2-Files.pdf
4HG19EC010HARSHITHAH
Ā 
PPTX
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
drkangurajuphd
Ā 
PPTX
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
alemunuruhak9
Ā 
PPTX
Python files / directories part15
Vishal Dutt
Ā 
PPTX
Chapter - 5.pptx
MikialeTesfamariam
Ā 
Python Session - 5
AnirudhaGaikwad4
Ā 
Python_AdvancedUnit - 3.pdf about the python
gpsign134
Ā 
File handling4.pdf
sulekha24
Ā 
UNIT –5.pptxpython for engineering students
SabarigiriVason
Ā 
File handling for reference class 12.pptx
PreeTVithule1
Ā 
Files in Python.pptx
Koteswari Kasireddy
Ā 
Files in Python.pptx
Koteswari Kasireddy
Ā 
File handling3.pdf
nishant874609
Ā 
Web Development Course: PHP lecture 3
Gheyath M. Othman
Ā 
pspp-rsk.pptx
ARYAN552812
Ā 
Python File Handling52616416416 ppt.pptx
saiwww3841k
Ā 
Python-files
Krishna Nanda
Ā 
python file handling
jhona2z
Ā 
File Handling as 08032021 (1).ppt
Raja Ram Dutta
Ā 
Module2-Files.pdf
4HG19EC010HARSHITHAH
Ā 
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
drkangurajuphd
Ā 
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
alemunuruhak9
Ā 
Python files / directories part15
Vishal Dutt
Ā 
Chapter - 5.pptx
MikialeTesfamariam
Ā 
Ad

More from Edureka! (20)

PDF
What to learn during the 21 days Lockdown | Edureka
Edureka!
Ā 
PDF
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
Ā 
PDF
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
Ā 
PDF
Tableau Tutorial for Data Science | Edureka
Edureka!
Ā 
PDF
Python Programming Tutorial | Edureka
Edureka!
Ā 
PDF
Top 5 PMP Certifications | Edureka
Edureka!
Ā 
PDF
Top Maven Interview Questions in 2020 | Edureka
Edureka!
Ā 
PDF
Linux Mint Tutorial | Edureka
Edureka!
Ā 
PDF
How to Deploy Java Web App in AWS| Edureka
Edureka!
Ā 
PDF
Importance of Digital Marketing | Edureka
Edureka!
Ā 
PDF
RPA in 2020 | Edureka
Edureka!
Ā 
PDF
Email Notifications in Jenkins | Edureka
Edureka!
Ā 
PDF
EA Algorithm in Machine Learning | Edureka
Edureka!
Ā 
PDF
Cognitive AI Tutorial | Edureka
Edureka!
Ā 
PDF
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
Ā 
PDF
Blue Prism Top Interview Questions | Edureka
Edureka!
Ā 
PDF
Big Data on AWS Tutorial | Edureka
Edureka!
Ā 
PDF
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
Ā 
PDF
Kubernetes Installation on Ubuntu | Edureka
Edureka!
Ā 
PDF
Introduction to DevOps | Edureka
Edureka!
Ā 
What to learn during the 21 days Lockdown | Edureka
Edureka!
Ā 
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
Ā 
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
Ā 
Tableau Tutorial for Data Science | Edureka
Edureka!
Ā 
Python Programming Tutorial | Edureka
Edureka!
Ā 
Top 5 PMP Certifications | Edureka
Edureka!
Ā 
Top Maven Interview Questions in 2020 | Edureka
Edureka!
Ā 
Linux Mint Tutorial | Edureka
Edureka!
Ā 
How to Deploy Java Web App in AWS| Edureka
Edureka!
Ā 
Importance of Digital Marketing | Edureka
Edureka!
Ā 
RPA in 2020 | Edureka
Edureka!
Ā 
Email Notifications in Jenkins | Edureka
Edureka!
Ā 
EA Algorithm in Machine Learning | Edureka
Edureka!
Ā 
Cognitive AI Tutorial | Edureka
Edureka!
Ā 
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
Ā 
Blue Prism Top Interview Questions | Edureka
Edureka!
Ā 
Big Data on AWS Tutorial | Edureka
Edureka!
Ā 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
Ā 
Kubernetes Installation on Ubuntu | Edureka
Edureka!
Ā 
Introduction to DevOps | Edureka
Edureka!
Ā 

Recently uploaded (20)

PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
PDF
Architecture of the Future (09152021)
EdwardMeyman
Ā 
PDF
Software Development Company | KodekX
KodekX
Ā 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
Ā 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
Ā 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
Ā 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
Ā 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
Ā 
PDF
Software Development Methodologies in 2025
KodekX
Ā 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
Ā 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
Ā 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
Ā 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
Ā 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
Architecture of the Future (09152021)
EdwardMeyman
Ā 
Software Development Company | KodekX
KodekX
Ā 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
Ā 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
Ā 
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
Ā 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
Ā 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
Ā 
Software Development Methodologies in 2025
KodekX
Ā 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
Ā 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
Ā 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
Ā 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
Ā 

Python File Handling | File Operations in Python | Learn python programming | Edureka

  • 1. Python Certification Training https://www.edureka.co/python Agenda File Handling In Python
  • 2. Python Certification Training https://www.edureka.co/python Agenda File Handling In Python
  • 3. Python Certification Training https://www.edureka.co/python Agenda Introduction 01 Why need File Handling? Getting Started 02 Concepts 03 Practical Approach 04 Types of Files Looking at code to understand theory Python File Handling System
  • 4. Python Certification Training https://www.edureka.co/python Why need File Handling? File Handling In Python
  • 5. Python Certification Training https://www.edureka.co/python Why Need File Handling? Python Input Arguments Standard Input Files
  • 6. Python Certification Training https://www.edureka.co/python Types of Files File Handling In Python
  • 7. Python Certification Training https://www.edureka.co/python Types Of Files What you may know as a file is slightly different in Python Text BinaryImage Text AudioExecutable
  • 8. Python Certification Training https://www.edureka.co/python What is File Handling? File Handling In Python
  • 9. Python Certification Training https://www.edureka.co/python What Is File Handling? File handling is an important part of any web application Operations Creating DeletionReading Updating CRUD
  • 10. Python Certification Training https://www.edureka.co/python Python File Handling System File Handling In Python
  • 11. Python Certification Training https://www.edureka.co/python Python File Handling System The key function for working with files in Python is the open() function open() Filename Mode Syntax open( filename, mode) WORK Create File Open File WORK Close File
  • 12. Python Certification Training https://www.edureka.co/python Python File Handling System The key function for working with files in Python is the open() function Syntax open( filename, mode) Any name that you want Different 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 "t" - Text - Default value. Text mode "b" - Binary - Binary mode (e.g. images) In addition you can specify if the file should be handled as binary or text mode
  • 13. Python Certification Training https://www.edureka.co/python Python File Handling System Example Code Example f = open(ā€œdemofile.txtā€) Example f = open(ā€œdemofile.txtā€, ā€œrā€) Note: Make sure file exists or else error!
  • 14. Python Certification Training https://www.edureka.co/python File Operations for Reading File Handling In Python
  • 15. Python Certification Training https://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.read() Lots of ways to read a text file in Python All characters Some characters
  • 16. Python Certification Training https://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.read() Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.read(5) This 5 indicates what?
  • 17. Python Certification Training https://www.edureka.co/python Reading Text File In Python file.read() Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.readline(): Line by line output Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.readline(3): Read third line only Example > file = open(ā€œtestfile.textā€, ā€œrā€) > print file.readlines(): Read lines separately
  • 18. Python Certification Training https://www.edureka.co/python Looping Over A File Object Fast and efficient! Example > file = open(ā€œtestfile.textā€, ā€œrā€) > for line in file: > print file.readline(): Looping over the object Reading from files
  • 19. Python Certification Training https://www.edureka.co/python Python File Write Method File Handling In Python
  • 20. Python Certification Training https://www.edureka.co/python File Write Method 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 Example > f = open("demofile.txt", "a") > f.write(ā€œ We love Edureka!") Example > f = open("demofile.txt", ā€œw") > f.write(ā€œ We love Edureka!") Note: the "w" method will overwrite the entire file.
  • 21. Python Certification Training https://www.edureka.co/python File Write Method Example > file = open(ā€œtestfile.txtā€, ā€œwā€) > file.write(ā€œThis is a testā€) > file.write(ā€œTo add more lines.ā€) > file.close() I’m writing files!
  • 22. Python Certification Training https://www.edureka.co/python Creating a New File File Handling In Python
  • 23. Python Certification Training https://www.edureka.co/python Creating A New File open() method again āž¢ file = open(ā€œtestfile.txtā€, ā€œxā€) āž¢ file = open(ā€œtestfile.txtā€, ā€œwā€) To create a new file in Python, use the open() method, with one of the following parameters: "x" - Create - will create a file, returns an error if the file exist "a" - Append - will create a file if the specified file does not exist "w" - Write - will create a file if the specified file does not exist
  • 24. Python Certification Training https://www.edureka.co/python Deletion Operations File Handling In Python
  • 25. Python Certification Training https://www.edureka.co/python Deleting A File os.remove() function To delete a file, you must import the OS module, and run its os.remove() function: Example > import os > os.remove("demofile.txt") Deleting a folder? Example > import os > os.rmdir("myfolder") Check if file exists > import os > if os.path.exists("demofile.txt"): > os.remove("demofile.txt") > else: > print("The file does not exist")
  • 26. Python Certification Training https://www.edureka.co/python Conclusion File Handling In Python
  • 27. Copyright Ā© 2019, edureka and/or its affiliates. All rights reserved.