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

PPTX
File handling in Python
PDF
Python programming : Files
PPTX
Data file handling in python reading & writing methods
PDF
File and directories in python
PPTX
Chapter 08 data file handling
PDF
Python File Handling | File Operations in Python | Learn python programming |...
PDF
PDF
File handling and Dictionaries in python
File handling in Python
Python programming : Files
Data file handling in python reading & writing methods
File and directories in python
Chapter 08 data file handling
Python File Handling | File Operations in Python | Learn python programming |...
File handling and Dictionaries in python

What's hot (20)

PDF
Python file handling
PPT
data file handling
PPTX
Data file handling in c++
PDF
Python - File operations & Data parsing
PPTX
File Handling Python
PDF
Python-files
PPT
File handling in C++
PPT
File Handling In C++(OOPs))
PPTX
basics of file handling
PPT
File handling in_c
PPTX
Data file handling in python introduction,opening & closing files
PPT
Filehandlinging cp2
PDF
Python-File handling-slides-pkt
PPSX
Files in c++
DOCX
File handling in c++
PDF
Python - Lecture 8
Python file handling
data file handling
Data file handling in c++
Python - File operations & Data parsing
File Handling Python
Python-files
File handling in C++
File Handling In C++(OOPs))
basics of file handling
File handling in_c
Data file handling in python introduction,opening & closing files
Filehandlinging cp2
Python-File handling-slides-pkt
Files in c++
File handling in c++
Python - Lecture 8
Ad

Similar to python file handling (20)

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

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Pre independence Education in Inndia.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pre independence Education in Inndia.pdf
GDM (1) (1).pptx small presentation for students
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Insiders guide to clinical Medicine.pdf
Computing-Curriculum for Schools in Ghana
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx

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.