SlideShare a Scribd company logo
2
Most read
3
Most read
21
Most read
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
File Handling
What is a File?
• A file represents a sequence of bytes on disk,
where a group of related data is stored.
• File is ready-made structure.
• File is created for permanent data storage (HDD).
• Syntax: file_object = open("filename", "mode")
File Modes
• r – opens a file in reading mode.
• w – opens/ creates a file in writing mode.
• x - opens a file for exclusive creation. If the file
already exists, the operation fails.
• a - opens a file in append mode.
• t - opens in text mode. (default)
• b - opens in binary mode.
• + - opens a file for updating (reading and writing)
File Modes
• In ‘append mode’, previous contents of file remain
& we can write more data after it (if file exists).
• In ‘write mode’, previous contents are cleared &
user writes from scratch(if file exists).
• A ‘Binary File’ is similar to text file. It deals with
non-text files like images or executable files..
Opening Files in Python
• Python has a built-in open() function to open a file.
• This function returns a file object, also called a
handle, as it is used to read or modify the file
accordingly.
• Ex: f = open("test.txt") #file in current directory
• f = open("C:/Python38/README.txt") # specifying
full path of file
Opening Files in Python
• f = open(“raw.txt") # equivalent to 'r' or 'rt'
• f = open(“raw.txt",'w') # write in text mode
• f = open("img.png",'r+b') # read and write in binary
mode
• f= open(“raw.txt","w+") #read and write permissions
Create a Text File
• f = open(“raw.txt","w+")
• Here, f is a variable to open a file.
• open() function is used here.
• raw.txt is a text file that has name ‘raw’.
• Permission is read and write.
• If file does not exist, file will be created.
Writing in a File
Reading from a File
• f = open(“raw.txt", 'r', encoding = 'utf-8')
• f.read(4) # read the first 4 data
• f.read(4) # read the next 4 data
• f.read() # read in the rest till end of file
• f.readline() #read individual lines of a file
Reading from a File
Reading from a File
Reading from a File
Read the whole file line by line:
Random Access - File
• There is no need to access the records of file
sequentially.
• To random access the files, there are 3 functions:
• f.seek()
• f.tell()
Random Access - Files
• It is used to seek pointer position in file at specified
byte. Its syntax is->
seek(offset, from = SEEK_SET)
• Changes the file position to offset bytes, in reference to
from (start, current, end).
• f.tell() – It returns value of current position in file. The value
is count from beginning of file. Its syntax-
f.tell()
Other Functions
• fileno() - Returns an integer number (file descriptor) of
the file.
• flush() – Flushes the write buffer of the file stream.
• seekable() - Returns True if the file stream supports
random access.
• truncate(size=None) - Resizes the file stream to size
bytes. If size is not specified, resizes to current
location.
• writable() - Returns True if the file stream can be
written to.
Close a File
• When we are done with performing operations on
the file, we need to properly close the file.
• Closing a file will free up the resources that were
tied with the file. It is done using the close()
method available in Python.
• Python has a garbage collector to clean up
unreferenced objects but we must not rely on it to
close the file.
Close a File
Close a File
• This method is not entirely safe.
• If an exception occurs when we are performing
some operation with the file, the code exits without
closing the file.
• A safer way is to use a try...finally block.
Close a File
• This way, we are guaranteeing that the file is
properly closed even if an exception is raised that
causes program flow to stop.
• The best way to close a file is by using the with
statement.
• We don't need to explicitly call the close() method. It
is done internally.
File Handling using Colab
• Upload the file:-
• Write from Colab:-
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

PPTX
File handling in Python
PDF
Python file handling
PPTX
Regular Expression
PDF
PPTX
Basics of Object Oriented Programming in Python
PDF
Strings in python
File handling in Python
Python file handling
Regular Expression
Basics of Object Oriented Programming in Python
Strings in python

What's hot (20)

PPTX
Variables in python
PDF
Datatypes in python
PPTX
File in C language
PDF
File and directories in python
PPTX
Python Functions
PPTX
Values and Data types in python
PPTX
Chapter 05 classes and objects
PDF
Python File Handling | File Operations in Python | Learn python programming |...
PPTX
Conditional and control statement
PDF
C++ OOPS Concept
PDF
Python programming : Files
PDF
Operators in python
PPT
File handling in c
PDF
Python Flow Control
PDF
Python programming : Classes objects
PPT
PPTX
Packages In Python Tutorial
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Functions in python slide share
Variables in python
Datatypes in python
File in C language
File and directories in python
Python Functions
Values and Data types in python
Chapter 05 classes and objects
Python File Handling | File Operations in Python | Learn python programming |...
Conditional and control statement
C++ OOPS Concept
Python programming : Files
Operators in python
File handling in c
Python Flow Control
Python programming : Classes objects
Packages In Python Tutorial
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Functions in python slide share
Ad

Similar to File Handling Python (20)

PPTX
pp5-filehandling- python 200824063109 (1).pptx
PDF
03-01-File Handling.pdf
PPTX
03-01-File Handling python.pptx
PPTX
pspp-rsk.pptx
PPTX
WjkjkjkjkjiohuhuhiuhkhiuhkjjjjjjjjjjK-12-13-file.pptx
PPTX
File Operations in python Read ,Write,binary file etc.
PDF
Python - Lecture 8
PPTX
5-filehandling-2004054567151830 (1).pptx
PDF
Unit 5 File handling in C programming.pdf
PPT
File Handling as 08032021 (1).ppt
PPTX
File handing in C
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
PPTX
What is FIle and explanation of text files.pptx
PPTX
Files in Python.pptx
PPTX
Files in Python.pptx
PPTX
File Handling.pptx
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
PPTX
File Handling in C
PPTX
File_Handling_in_C_Programming for First Year Engineering
PPTX
files concept in c program which is very use full
pp5-filehandling- python 200824063109 (1).pptx
03-01-File Handling.pdf
03-01-File Handling python.pptx
pspp-rsk.pptx
WjkjkjkjkjiohuhuhiuhkhiuhkjjjjjjjjjjK-12-13-file.pptx
File Operations in python Read ,Write,binary file etc.
Python - Lecture 8
5-filehandling-2004054567151830 (1).pptx
Unit 5 File handling in C programming.pdf
File Handling as 08032021 (1).ppt
File handing in C
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
What is FIle and explanation of text files.pptx
Files in Python.pptx
Files in Python.pptx
File Handling.pptx
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File Handling in C
File_Handling_in_C_Programming for First Year Engineering
files concept in c program which is very use full
Ad

More from Akhil Kaushik (20)

PPT
Symbol Table, Error Handler & Code Generation
PPTX
Code Optimization
PPTX
Parsing in Compiler Design
PPTX
Context Free Grammar
PPTX
Error Detection & Recovery
PPTX
Symbol Table
PPTX
Lexical Analyzer Implementation
PPTX
NFA & DFA
PPTX
Lexical Analysis - Compiler Design
PPTX
Regular Expressions
PPTX
Algorithms & Complexity Calculation
PPTX
Intro to Data Structure & Algorithms
PPTX
Decision Making & Loops
PPTX
Basic programs in Python
PPTX
Python Data-Types
PPTX
Introduction to Python Programming
PPT
Compiler Design Basics
PPTX
Bootstrapping in Compiler
PPTX
Compiler construction tools
PPTX
Phases of compiler
Symbol Table, Error Handler & Code Generation
Code Optimization
Parsing in Compiler Design
Context Free Grammar
Error Detection & Recovery
Symbol Table
Lexical Analyzer Implementation
NFA & DFA
Lexical Analysis - Compiler Design
Regular Expressions
Algorithms & Complexity Calculation
Intro to Data Structure & Algorithms
Decision Making & Loops
Basic programs in Python
Python Data-Types
Introduction to Python Programming
Compiler Design Basics
Bootstrapping in Compiler
Compiler construction tools
Phases of compiler

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Basic Mud Logging Guide for educational purpose
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Institutional Correction lecture only . . .
human mycosis Human fungal infections are called human mycosis..pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O5-L3 Freight Transport Ops (International) V1.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Types and Its function , kingdom of life
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Basic Mud Logging Guide for educational purpose
RMMM.pdf make it easy to upload and study
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Institutional Correction lecture only . . .

File Handling Python

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani File Handling
  • 2. What is a File? • A file represents a sequence of bytes on disk, where a group of related data is stored. • File is ready-made structure. • File is created for permanent data storage (HDD). • Syntax: file_object = open("filename", "mode")
  • 3. File Modes • r – opens a file in reading mode. • w – opens/ creates a file in writing mode. • x - opens a file for exclusive creation. If the file already exists, the operation fails. • a - opens a file in append mode. • t - opens in text mode. (default) • b - opens in binary mode. • + - opens a file for updating (reading and writing)
  • 4. File Modes • In ‘append mode’, previous contents of file remain & we can write more data after it (if file exists). • In ‘write mode’, previous contents are cleared & user writes from scratch(if file exists). • A ‘Binary File’ is similar to text file. It deals with non-text files like images or executable files..
  • 5. Opening Files in Python • Python has a built-in open() function to open a file. • This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. • Ex: f = open("test.txt") #file in current directory • f = open("C:/Python38/README.txt") # specifying full path of file
  • 6. Opening Files in Python • f = open(“raw.txt") # equivalent to 'r' or 'rt' • f = open(“raw.txt",'w') # write in text mode • f = open("img.png",'r+b') # read and write in binary mode • f= open(“raw.txt","w+") #read and write permissions
  • 7. Create a Text File • f = open(“raw.txt","w+") • Here, f is a variable to open a file. • open() function is used here. • raw.txt is a text file that has name ‘raw’. • Permission is read and write. • If file does not exist, file will be created.
  • 9. Reading from a File • f = open(“raw.txt", 'r', encoding = 'utf-8') • f.read(4) # read the first 4 data • f.read(4) # read the next 4 data • f.read() # read in the rest till end of file • f.readline() #read individual lines of a file
  • 12. Reading from a File Read the whole file line by line:
  • 13. Random Access - File • There is no need to access the records of file sequentially. • To random access the files, there are 3 functions: • f.seek() • f.tell()
  • 14. Random Access - Files • It is used to seek pointer position in file at specified byte. Its syntax is-> seek(offset, from = SEEK_SET) • Changes the file position to offset bytes, in reference to from (start, current, end). • f.tell() – It returns value of current position in file. The value is count from beginning of file. Its syntax- f.tell()
  • 15. Other Functions • fileno() - Returns an integer number (file descriptor) of the file. • flush() – Flushes the write buffer of the file stream. • seekable() - Returns True if the file stream supports random access. • truncate(size=None) - Resizes the file stream to size bytes. If size is not specified, resizes to current location. • writable() - Returns True if the file stream can be written to.
  • 16. Close a File • When we are done with performing operations on the file, we need to properly close the file. • Closing a file will free up the resources that were tied with the file. It is done using the close() method available in Python. • Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.
  • 18. Close a File • This method is not entirely safe. • If an exception occurs when we are performing some operation with the file, the code exits without closing the file. • A safer way is to use a try...finally block.
  • 19. Close a File • This way, we are guaranteeing that the file is properly closed even if an exception is raised that causes program flow to stop. • The best way to close a file is by using the with statement. • We don't need to explicitly call the close() method. It is done internally.
  • 20. File Handling using Colab • Upload the file:- • Write from Colab:-
  • 21. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!