SlideShare a Scribd company logo
FILE HANDLING IN PYTHON
Vinay Agrawal
ASST. PROFESSOR
CEA DEPT.
GLA UNIVERSITY,
MATHURA
File Operations
1. open()
Example
2. close()
3. write()
4. read()
Other functions
 To read contents from file
1. fp.read(n) :- n is the number of bytes to read
2. fp.readline():- It will read only one line that is
point by fp.
3. fp.readlines(): It will all the lines of the file and
store all the lines in list
 To write contents into the file
1. fp.writelines(l):- It will write all the lines present
in the list l
Programs
1. WAP to write starting 10 even numbers in the
output file output1.txt ( in single line)
2. WAP to write starting 10 odd numbers in the
output file output2.txt ( in multiple lines)
3. WAP to write 5 random numbers between 50-
60(both inclusive) in the output file output3.txt ( in
single line)
4. WAP that reads data from a file and calculates the
percentage of vowels and consonants in the file
Prgrams
5. WAP that accepts filename as an input from the
user. Open the file and count the number of times
a character(I/P from user) appears in the file
6. WAP that copies one Python Script into another in
such a way that all comment lines are skipped and
not copied in the destination file.
7. WAP to enter the name of the file and print the
longest line present in the file.
8. Write a Python program to read a file (“input.txt”) of
oil prices in England , Australia and India. Output the
average oil price of each country to an output file
output.txt.(Note: All fields are separated with one
space)
Modes of opening the file
Text File Binary File
r rb
w wb
a ab
r+ rb+
w+ wb+
a+ ab+
x xb
with keyword
We can open the file using with keyword.
Example:
s=fp.read()
The advantage of using with keyword is “File is
properly closed after it is used even if an error
occurs during read or write operation or even when
you forget to explicitly close the file”
with open(“a.txt“,”r”) as fp:
s=fp.read()
print(s)
Other useful File methods
 fp.fileno() :- It returns the file number of the file (File
Descriptor)
 fp.truncate(n) :- Resizes the file to n bytes
 fp.tell():- It tells the current position of the file
pointer
 fp.seek(byteoffset,reference) :-It is used to move
the file pointer fp to a new location.
Assignment
1. WAP that reads a file line by line. Each line read from the file is copied to
another file with line numbers specified at the beginning of the line.
For example, if contents of file is as follows:
Hello
Bye
GLA
Expected O/P:
1. Hello
2. Bye
3. GLA
2. WAP that counts the number of tabs, spaces and
newline characters in a file
3. WAP that counts the number of characters, words and
lines in a file
4. WAP that generates a Quiz and uses two files-
Questions.txt and Answers.txt . The program opens
Questions.txt and reads a question and displays the
question with options on the screen. The program then
opens the Answers.txt file and displays the correct
answers.
5. WAP to read first n lines from the file.
6. WAP to read last n lines from the file.
7. WAP to read from m line to n line.
8. WAP to write all the prime numbers from 2 to 100
to the output file output1.txt
9. WAP to append all the armstrong numbers from
100 to 999 to the above file outfile1.txt
10. WAP to write only those lines in the output file that starts with 172
Input File
Hello Raj
172.16.42.35
165.72.42.1
127.0.01
172.12.12.10
17.13.11.10
Expected O/P:
172.16.42.35
172.12.12.10

More Related Content

PPTX
File handling in c
PPT
file handling1
PPTX
Data file handling in python reading & writing methods
PPTX
Data file handling in python reading & writing methods
PPTX
C Programming Unit-5
PPS
C programming session 08
DOCX
Understanding c file handling functions with examples
PPT
file.ppt
File handling in c
file handling1
Data file handling in python reading & writing methods
Data file handling in python reading & writing methods
C Programming Unit-5
C programming session 08
Understanding c file handling functions with examples
file.ppt

Similar to File Handling in Python Programming .ppt (20)

PDF
4 text file
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
PPS PPT 2.pptx
PDF
Handout#01
PPTX
FILE INPUT OUTPUT.pptx
PPT
file_handling_in_c.ppt
PPTX
Unit-VI.pptx
PDF
FILES IN C
PPTX
File management
PDF
Module 5 file cp
DOCX
Unit 5 dwqb ans
PPTX
Programming C- File Handling , File Operation
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PDF
Module2-Files.pdf
PPTX
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
PPTX
Python files / directories part16
PPTX
Programming in C
DOCX
PPTX
PPS-II UNIT-5 PPT.pptx
PPTX
DFH PDF-converted.pptx
4 text file
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPS PPT 2.pptx
Handout#01
FILE INPUT OUTPUT.pptx
file_handling_in_c.ppt
Unit-VI.pptx
FILES IN C
File management
Module 5 file cp
Unit 5 dwqb ans
Programming C- File Handling , File Operation
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Module2-Files.pdf
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
Python files / directories part16
Programming in C
PPS-II UNIT-5 PPT.pptx
DFH PDF-converted.pptx
Ad

More from vinayagrawal71 (8)

PPT
Recursion and Lambda Functions in python.ppt
PPT
Sets in the python programming language.ppt
PPT
File Handling in python programming .ppt
PPTX
Exceptional Handling in the python .pptx
PPTX
Exception Handling in Python programming.pptx
PPT
Dictionarys in python programming language.ppt
PPTX
11Exceptional Handling In Python Language.pptx
PPTX
Exception Handling in Python Programming.pptx
Recursion and Lambda Functions in python.ppt
Sets in the python programming language.ppt
File Handling in python programming .ppt
Exceptional Handling in the python .pptx
Exception Handling in Python programming.pptx
Dictionarys in python programming language.ppt
11Exceptional Handling In Python Language.pptx
Exception Handling in Python Programming.pptx
Ad

Recently uploaded (20)

PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPT
Drone Technology Electronics components_1
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
anatomy of limbus and anterior chamber .pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
Simulation of electric circuit laws using tinkercad.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
436813905-LNG-Process-Overview-Short.pptx
Internship_Presentation_Final engineering.pptx
Structs to JSON How Go Powers REST APIs.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Drone Technology Electronics components_1
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
anatomy of limbus and anterior chamber .pptx
bas. eng. economics group 4 presentation 1.pptx

File Handling in Python Programming .ppt

  • 1. FILE HANDLING IN PYTHON Vinay Agrawal ASST. PROFESSOR CEA DEPT. GLA UNIVERSITY, MATHURA
  • 8. Other functions  To read contents from file 1. fp.read(n) :- n is the number of bytes to read 2. fp.readline():- It will read only one line that is point by fp. 3. fp.readlines(): It will all the lines of the file and store all the lines in list  To write contents into the file 1. fp.writelines(l):- It will write all the lines present in the list l
  • 9. Programs 1. WAP to write starting 10 even numbers in the output file output1.txt ( in single line) 2. WAP to write starting 10 odd numbers in the output file output2.txt ( in multiple lines) 3. WAP to write 5 random numbers between 50- 60(both inclusive) in the output file output3.txt ( in single line) 4. WAP that reads data from a file and calculates the percentage of vowels and consonants in the file
  • 10. Prgrams 5. WAP that accepts filename as an input from the user. Open the file and count the number of times a character(I/P from user) appears in the file 6. WAP that copies one Python Script into another in such a way that all comment lines are skipped and not copied in the destination file. 7. WAP to enter the name of the file and print the longest line present in the file.
  • 11. 8. Write a Python program to read a file (“input.txt”) of oil prices in England , Australia and India. Output the average oil price of each country to an output file output.txt.(Note: All fields are separated with one space)
  • 12. Modes of opening the file Text File Binary File r rb w wb a ab r+ rb+ w+ wb+ a+ ab+ x xb
  • 13. with keyword We can open the file using with keyword. Example: s=fp.read() The advantage of using with keyword is “File is properly closed after it is used even if an error occurs during read or write operation or even when you forget to explicitly close the file” with open(“a.txt“,”r”) as fp: s=fp.read() print(s)
  • 14. Other useful File methods  fp.fileno() :- It returns the file number of the file (File Descriptor)  fp.truncate(n) :- Resizes the file to n bytes  fp.tell():- It tells the current position of the file pointer  fp.seek(byteoffset,reference) :-It is used to move the file pointer fp to a new location.
  • 15. Assignment 1. WAP that reads a file line by line. Each line read from the file is copied to another file with line numbers specified at the beginning of the line. For example, if contents of file is as follows: Hello Bye GLA Expected O/P: 1. Hello 2. Bye 3. GLA
  • 16. 2. WAP that counts the number of tabs, spaces and newline characters in a file 3. WAP that counts the number of characters, words and lines in a file 4. WAP that generates a Quiz and uses two files- Questions.txt and Answers.txt . The program opens Questions.txt and reads a question and displays the question with options on the screen. The program then opens the Answers.txt file and displays the correct answers.
  • 17. 5. WAP to read first n lines from the file. 6. WAP to read last n lines from the file. 7. WAP to read from m line to n line. 8. WAP to write all the prime numbers from 2 to 100 to the output file output1.txt 9. WAP to append all the armstrong numbers from 100 to 999 to the above file outfile1.txt
  • 18. 10. WAP to write only those lines in the output file that starts with 172 Input File Hello Raj 172.16.42.35 165.72.42.1 127.0.01 172.12.12.10 17.13.11.10 Expected O/P: 172.16.42.35 172.12.12.10