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
Ad

More Related Content

What's hot (20)

File handling in Python
File handling in PythonFile handling in Python
File handling in Python
BMS Institute of Technology and Management
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
Emertxe Information Technologies Pvt Ltd
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
Karim Sonbol
 
Python-Functions.pptx
Python-Functions.pptxPython-Functions.pptx
Python-Functions.pptx
Karudaiyar Ganapathy
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
Python basics
Python basicsPython basics
Python basics
RANAALIMAJEEDRAJPUT
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
International Institute of Information Technology (I²IT)
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
Karim Sonbol
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 

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

Python Session - 5
Python Session - 5Python Session - 5
Python Session - 5
AnirudhaGaikwad4
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
sulekha24
 
UNIT –5.pptxpython for engineering students
UNIT –5.pptxpython for engineering studentsUNIT –5.pptxpython for engineering students
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
File handling for reference class 12.pptx
File handling for reference class 12.pptxFile handling for reference class 12.pptx
File handling for reference class 12.pptx
PreeTVithule1
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
nishant874609
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
ARYAN552812
 
Python File Handling52616416416 ppt.pptx
Python File Handling52616416416 ppt.pptxPython File Handling52616416416 ppt.pptx
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
Kongunadu College of Engineering and Technology
 
Python-files
Python-filesPython-files
Python-files
Krishna Nanda
 
python file handling
python file handlingpython file handling
python file handling
jhona2z
 
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
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
UNIT 5 PY.pptx   -  FILE HANDLING CONCEPTSUNIT 5 PY.pptx   -  FILE HANDLING CONCEPTS
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
drkangurajuphd
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15
Vishal Dutt
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
MikialeTesfamariam
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
Vishal Dutt
 
this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
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
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
sulekha24
 
UNIT –5.pptxpython for engineering students
UNIT –5.pptxpython for engineering studentsUNIT –5.pptxpython for engineering students
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
File handling for reference class 12.pptx
File handling for reference class 12.pptxFile handling for reference class 12.pptx
File handling for reference class 12.pptx
PreeTVithule1
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
Python File Handling52616416416 ppt.pptx
Python File Handling52616416416 ppt.pptxPython File Handling52616416416 ppt.pptx
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
python file handling
python file handlingpython file handling
python file handling
jhona2z
 
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
 
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
UNIT 5 PY.pptx   -  FILE HANDLING CONCEPTSUNIT 5 PY.pptx   -  FILE HANDLING CONCEPTS
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
drkangurajuphd
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15
Vishal Dutt
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
Vishal Dutt
 
this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
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
 
Ad

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
Ad

Recently uploaded (20)

ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 

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.