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

What's hot (20)

Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
FellowBuddy.com
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
baabtra.com - No. 1 supplier of quality freshers
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
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
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
nitamhaske
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
baabtra.com - No. 1 supplier of quality freshers
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
FellowBuddy.com
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
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
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
nitamhaske
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
 

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
 
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
 
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
4-chapter-File & Directores.pptx debre CTABOUR UNIversit4-chapter-File & Directores.pptx debre CTABOUR UNIversit
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
alemunuruhak9
 
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
 
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
 
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
4-chapter-File & Directores.pptx debre CTABOUR UNIversit4-chapter-File & Directores.pptx debre CTABOUR UNIversit
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
alemunuruhak9
 
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
 
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)

Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 

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.