SlideShare a Scribd company logo
Python File Handling: Create, Open,
Append, Read, Write
File handling a method to store the output of the program to a file or take input from the file. File
handling is a key concept in the programming world. File handling used in almost every kind of
project. For example, you are creating an Inventory management system. In the inventory
management system, you have data related to sales and purchases so you need to store that
data somewhere. Using file handling you can store that data into the file. If you are trying to
perform some analysis on the data then you must be provided with data in the form comma
separated file or Microsoft Excel file. Using file handling you can read data from that file and
also store output back into that file.
While using file handling there are different types of modes available. Just have a look at them
we will explain them in detail.
● “ r “, for reading.
● “ w “, for writing.
● “ a “, for appending.
● “ r+ “, for both reading and writing
Create file
Let’s take a look at how to create a file using Python programming language.
Take a look at the left side we have only hello.py file.
Now let’s execute the code.
f = open("data.txt","w+")
w+ stands for: Open a file for writing and reading.
The file with name data.txt has been created.
Open and write into file
We will be using the above-created file. Let’s write some data into the file using code.
f = open("data.txt","w+")
for i in range(5):
f.write("line " + str(i) + "n")
The file looks like this
Append into file
“Write” option erases all the previous data present in the file. For appending data into an
existing file we need to use a mode of “a”.
Let’s take a look at the code.
f = open("data.txt","a")
for i in range(2):
f.write("appended lines " + str(i) + "n")
The data.txt file will look like this
Read data from file
Let’s take a look at code to read data from the data.txt file.
f.read function output all the data present in the file.
f = open("data.txt","r")
print(f.read())
If we need to read the data line by line we use readline() function.
For reading first two lines.

More Related Content

What's hot (20)

PDF
Python file handling
Prof. Dr. K. Adisesha
 
PPT
data file handling
krishna partiwala
 
PPTX
Data file handling in c++
Vineeta Garg
 
PDF
Python - File operations & Data parsing
Felix Z. Hoffmann
 
PPTX
File Handling Python
Akhil Kaushik
 
PDF
Python-files
Krishna Nanda
 
PPT
File handling in C++
Hitesh Kumar
 
PPT
File Handling In C++(OOPs))
Papu Kumar
 
PPTX
basics of file handling
pinkpreet_kaur
 
PPT
File handling in_c
sanya6900
 
PPTX
Data file handling in python introduction,opening & closing files
keeeerty
 
PPT
Filehandlinging cp2
Tanmay Baranwal
 
PDF
Python-File handling-slides-pkt
Pradyumna Tripathy
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
DOCX
File handling in c++
Daniel Nyagechi
 
PDF
Python - Lecture 8
Ravi Kiran Khareedi
 
Python file handling
Prof. Dr. K. Adisesha
 
data file handling
krishna partiwala
 
Data file handling in c++
Vineeta Garg
 
Python - File operations & Data parsing
Felix Z. Hoffmann
 
File Handling Python
Akhil Kaushik
 
Python-files
Krishna Nanda
 
File handling in C++
Hitesh Kumar
 
File Handling In C++(OOPs))
Papu Kumar
 
basics of file handling
pinkpreet_kaur
 
File handling in_c
sanya6900
 
Data file handling in python introduction,opening & closing files
keeeerty
 
Filehandlinging cp2
Tanmay Baranwal
 
Python-File handling-slides-pkt
Pradyumna Tripathy
 
Files in c++
Selvin Josy Bai Somu
 
File handling in c++
Daniel Nyagechi
 
Python - Lecture 8
Ravi Kiran Khareedi
 

Similar to python file handling (20)

PPTX
Chapter - 5.pptx
MikialeTesfamariam
 
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
PPTX
Unit V.pptx
ShaswatSurya
 
PPTX
FILE HANDLING.pptx
kendriyavidyalayano24
 
PPT
Python File functions
keerthanakommera1
 
PDF
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
PPTX
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
PPTX
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
PPTX
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
PDF
file handling.pdf
RonitVaskar2
 
PPTX
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
PDF
03-01-File Handling.pdf
botin17097
 
PPTX
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
PPTX
what are python basics.pptx.Join Python training in Chandigarh
asmeerana605
 
PDF
File handling3.pdf
nishant874609
 
PDF
FileHandling2023_Text File.pdf CBSE 2014
JAYASURYANSHUPEDDAPA
 
PDF
chapter-4-data-file-handlingeng.pdf
SyedAhmed991492
 
PPTX
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
Chapter - 5.pptx
MikialeTesfamariam
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
Unit V.pptx
ShaswatSurya
 
FILE HANDLING.pptx
kendriyavidyalayano24
 
Python File functions
keerthanakommera1
 
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
file handling.pdf
RonitVaskar2
 
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
03-01-File Handling.pdf
botin17097
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
what are python basics.pptx.Join Python training in Chandigarh
asmeerana605
 
File handling3.pdf
nishant874609
 
FileHandling2023_Text File.pdf CBSE 2014
JAYASURYANSHUPEDDAPA
 
chapter-4-data-file-handlingeng.pdf
SyedAhmed991492
 
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
Ad

Recently uploaded (20)

PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Horarios de distribución de agua en julio
pegazohn1978
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Ad

python file handling

  • 1. Python File Handling: Create, Open, Append, Read, Write File handling a method to store the output of the program to a file or take input from the file. File handling is a key concept in the programming world. File handling used in almost every kind of project. For example, you are creating an Inventory management system. In the inventory management system, you have data related to sales and purchases so you need to store that data somewhere. Using file handling you can store that data into the file. If you are trying to perform some analysis on the data then you must be provided with data in the form comma separated file or Microsoft Excel file. Using file handling you can read data from that file and also store output back into that file. While using file handling there are different types of modes available. Just have a look at them we will explain them in detail. ● “ r “, for reading. ● “ w “, for writing. ● “ a “, for appending. ● “ r+ “, for both reading and writing Create file Let’s take a look at how to create a file using Python programming language. Take a look at the left side we have only hello.py file. Now let’s execute the code. f = open("data.txt","w+")
  • 2. w+ stands for: Open a file for writing and reading. The file with name data.txt has been created. Open and write into file We will be using the above-created file. Let’s write some data into the file using code. f = open("data.txt","w+") for i in range(5): f.write("line " + str(i) + "n")
  • 3. The file looks like this Append into file “Write” option erases all the previous data present in the file. For appending data into an existing file we need to use a mode of “a”. Let’s take a look at the code. f = open("data.txt","a") for i in range(2): f.write("appended lines " + str(i) + "n") The data.txt file will look like this
  • 4. Read data from file Let’s take a look at code to read data from the data.txt file. f.read function output all the data present in the file. f = open("data.txt","r") print(f.read()) If we need to read the data line by line we use readline() function.
  • 5. For reading first two lines.