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

PDF
PPTX
Data file handling in python introduction,opening & closing files
PDF
File and directories in python
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
String Manipulation in Python
PDF
Python file handling
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PDF
Function arguments In Python
Data file handling in python introduction,opening & closing files
File and directories in python
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
String Manipulation in Python
Python file handling
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Function arguments In Python

What's hot (20)

PPTX
File Handling Python
PPT
Python GUI Programming
PDF
Python programming : Files
PPTX
Chapter 03 python libraries
PPTX
Introduction to the basics of Python programming (part 1)
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PDF
Datatypes in python
PPTX
Python Scipy Numpy
PPTX
Python Tutorial Part 1
PDF
PDF
Python set
PPTX
Python Libraries and Modules
PPTX
Python-Inheritance.pptx
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
PPT
Python Dictionaries and Sets
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PPTX
Modules in Python Programming
PPTX
Python basics
File Handling Python
Python GUI Programming
Python programming : Files
Chapter 03 python libraries
Introduction to the basics of Python programming (part 1)
What is Python Lambda Function? Python Tutorial | Edureka
Datatypes in python
Python Scipy Numpy
Python Tutorial Part 1
Python set
Python Libraries and Modules
Python-Inheritance.pptx
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Python Dictionaries and Sets
Python Class | Python Programming | Python Tutorial | Edureka
Modules in Python Programming
Python basics
Ad

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

PDF
Python-files
PPTX
Chapter - 5.pptx
PDF
Module2-Files.pdf
PPTX
this is about file concepts in class 12 in python , text file, binary file, c...
PPTX
Python File Handling52616416416 ppt.pptx
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
PDF
file handling.pdf
PPTX
What is FIle and explanation of text files.pptx
PPTX
for interview this ppt is a teching aid for file handling concepts includes t...
PPTX
File Handling in Python -binary files.pptx
PPTX
5-filehandling-2004054567151830 (1).pptx
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
PPTX
FILES CONCEPTS IN PYTHON PROGRAMMING.pptx
PPTX
Python files creation read,write Unit 5.pptx
PDF
FileHandling2023_Text File.pdf CBSE 2014
PPTX
file handlling in python 23.12.24 geetha.pptx
PPTX
Unit V.pptx
PPTX
The Concepts of File Handling in Python.
PDF
File handling & regular expressions in python programming
PPTX
FILE HANDLING.pptx
Python-files
Chapter - 5.pptx
Module2-Files.pdf
this is about file concepts in class 12 in python , text file, binary file, c...
Python File Handling52616416416 ppt.pptx
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
file handling.pdf
What is FIle and explanation of text files.pptx
for interview this ppt is a teching aid for file handling concepts includes t...
File Handling in Python -binary files.pptx
5-filehandling-2004054567151830 (1).pptx
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
FILES CONCEPTS IN PYTHON PROGRAMMING.pptx
Python files creation read,write Unit 5.pptx
FileHandling2023_Text File.pdf CBSE 2014
file handlling in python 23.12.24 geetha.pptx
Unit V.pptx
The Concepts of File Handling in Python.
File handling & regular expressions in python programming
FILE HANDLING.pptx
Ad

More from Edureka! (20)

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

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PPTX
Spectroscopy.pptx food analysis technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Getting Started with Data Integration: FME Form 101
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
Spectroscopy.pptx food analysis technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf

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.