SlideShare a Scribd company logo
Files Handling
Files
• Most file methods are concerned with performing input
from and output to the external file associated with a file
object, but other file methods allow us to seek to a new
position in the file, flush output buffers, and so on. Table
summarizes common file operations.
Files handling using python language.pptx
Opening Files
• To open a file, a program calls the built-in open function, with
the external filename first, followed by a processing mode. The
call returns a file object, which in turn has methods for data
transfer:
• The first argument to open, the external filename, may include
a platform-specific and absolute or relative directory path
prefix. Without a directory path, the file is assumed to exist in
the current working directory (i.e., where the script runs).
• The second argument to open, processing mode, is typically the
string 'r' to open for text input (the default), 'w' to create and
open for text output, or 'a' to open for appending text to the
end (e.g., for adding to logfiles).
• The processing mode argument can specify additional options:
 • Adding a b to the mode string allows for binary data (end-of-
line translations and 3.X Unicode encodings are turned off).
• Adding a + opens the file for both input and output (i.e., you
can both read and write to the same file object, often in
conjunction with seek operations to reposition in the file).
Both of the first two arguments to open must be Python strings.
An optional third argument can be used to control output
buffering—passing a zero means that output is unbuffered
• Once you make a file object with open, you can call its
methods to read from or write to the associated external
file. In all cases, file text takes the form of strings in Python
programs; reading a file returns its content in strings, and
content is passed to the write methods as strings.
• File iterators are best for reading lines
• Content is strings, not objects: data read from a file always
comes back to your script as a string, so you’ll have to
convert it to a different type of Python object if a string is
not what you need.
• The following code begins by opening a new text file for
output, writing two lines (strings terminated with a newline
marker, n), and closing the file.
• If you want to display the file’s content with end-of-line
characters interpreted, read the entire file into a string all at
once with the file object’s read method and print it:
Storing Python Objects in Files:
Conversions
• convert objects to strings using conversion tools. Again, file
data is always strings in our scripts, and write methods do
not do any automatic to-string formatting for us
• the interactive echo gives the exact byte contents, while the
print operation interprets embedded end-of-line characters
to render a more user-friendly display:
We now have to use other conversion tools to translate from the
strings in the text file to real Python objects. As Python never converts
strings to numbers (or other types of objects) automatically, this is
required if we need to gain access to normal object tools like indexing,
addition, and so on:
For this first line, we used the string rstrip method to get rid of the trailing
end-of-line character; a line[: 1]
− slice would work, too, but only if we can be
sure all lines end in the n character (the last line in a file sometimes does
not).
We still must convert from strings to integers, though, if we wish to perform math on these:
int translates a string of digits into an integer object.
Notice that we didn’t have to run rstrip to delete the n at the end of the
last part; int and some other converters quietly ignore whitespace around
digits.
Finally, to convert the stored list and dictionary in the third line of the file,
we can run them through eval, a built-in function that treats a string as a
piece of executable program code
Files handling using python language.pptx
Storing Native Python Objects: pickle
• If you really want to store native Python objects, but you can’t trust the source of the data in
the file, Python’s standard library pickle module is ideal.
• The pickle module is a more advanced tool that allows us to store almost any Python object in
a file directly, with no to- or from-string conversion requirement on our part. It’s like a super-
general data formatting and parsing utility.
• To store a dictionary in a file, for instance, we pickle it directly:
Then, to get the dictionary back later, we simply use pickle again to re-create it:
Files handling using python language.pptx
Ad

Recommended

Unit V.pptx
Unit V.pptx
ShaswatSurya
 
01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
File handling in Python
File handling in Python
Megha V
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
Blue Elephant Consulting
 
Python 01.pptx
Python 01.pptx
AliMohammadAmiri
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx
qover
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
Files in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
anushasabhapathy76
 
Python file handling
Python file handling
Prof. Dr. K. Adisesha
 
03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 
File handling & regular expressions in python programming
File handling & regular expressions in python programming
Srinivas Narasegouda
 
File handling with python class 12th .pdf
File handling with python class 12th .pdf
lionsconvent1234
 
Python-files
Python-files
Krishna Nanda
 
File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
Module2-Files.pdf
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
file handling in python using exception statement
file handling in python using exception statement
srividhyaarajagopal
 
File handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
File Handling in Python -binary files.pptx
File Handling in Python -binary files.pptx
deepa63690
 
Python-FileHandling.pptx
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
Python file handlings
Python file handlings
22261A1201ABDULMUQTA
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
File handling in Python this PPT gives
File handling in Python this PPT gives
pankajdesai217634
 
Python Course In Chandigarh
Python Course In Chandigarh
Excellence Academy
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 

More Related Content

Similar to Files handling using python language.pptx (20)

5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
Files in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
anushasabhapathy76
 
Python file handling
Python file handling
Prof. Dr. K. Adisesha
 
03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 
File handling & regular expressions in python programming
File handling & regular expressions in python programming
Srinivas Narasegouda
 
File handling with python class 12th .pdf
File handling with python class 12th .pdf
lionsconvent1234
 
Python-files
Python-files
Krishna Nanda
 
File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
Module2-Files.pdf
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
file handling in python using exception statement
file handling in python using exception statement
srividhyaarajagopal
 
File handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
File Handling in Python -binary files.pptx
File Handling in Python -binary files.pptx
deepa63690
 
Python-FileHandling.pptx
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
Python file handlings
Python file handlings
22261A1201ABDULMUQTA
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
File handling in Python this PPT gives
File handling in Python this PPT gives
pankajdesai217634
 
Python Course In Chandigarh
Python Course In Chandigarh
Excellence Academy
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
anushasabhapathy76
 
03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 
File handling & regular expressions in python programming
File handling & regular expressions in python programming
Srinivas Narasegouda
 
File handling with python class 12th .pdf
File handling with python class 12th .pdf
lionsconvent1234
 
File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
file handling in python using exception statement
file handling in python using exception statement
srividhyaarajagopal
 
File handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
this is about file concepts in class 12 in python , text file, binary file, c...
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
File Handling in Python -binary files.pptx
File Handling in Python -binary files.pptx
deepa63690
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
File handling in Python this PPT gives
File handling in Python this PPT gives
pankajdesai217634
 

Recently uploaded (20)

Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
Jamie Coleman
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
NEW-IDM Crack with Internet Download Manager 6.42 Build 27 VERSION
NEW-IDM Crack with Internet Download Manager 6.42 Build 27 VERSION
grete1122g
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
Jamie Coleman
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
NEW-IDM Crack with Internet Download Manager 6.42 Build 27 VERSION
NEW-IDM Crack with Internet Download Manager 6.42 Build 27 VERSION
grete1122g
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
Ad

Files handling using python language.pptx

  • 2. Files • Most file methods are concerned with performing input from and output to the external file associated with a file object, but other file methods allow us to seek to a new position in the file, flush output buffers, and so on. Table summarizes common file operations.
  • 4. Opening Files • To open a file, a program calls the built-in open function, with the external filename first, followed by a processing mode. The call returns a file object, which in turn has methods for data transfer: • The first argument to open, the external filename, may include a platform-specific and absolute or relative directory path prefix. Without a directory path, the file is assumed to exist in the current working directory (i.e., where the script runs).
  • 5. • The second argument to open, processing mode, is typically the string 'r' to open for text input (the default), 'w' to create and open for text output, or 'a' to open for appending text to the end (e.g., for adding to logfiles). • The processing mode argument can specify additional options:  • Adding a b to the mode string allows for binary data (end-of- line translations and 3.X Unicode encodings are turned off). • Adding a + opens the file for both input and output (i.e., you can both read and write to the same file object, often in conjunction with seek operations to reposition in the file). Both of the first two arguments to open must be Python strings. An optional third argument can be used to control output buffering—passing a zero means that output is unbuffered
  • 6. • Once you make a file object with open, you can call its methods to read from or write to the associated external file. In all cases, file text takes the form of strings in Python programs; reading a file returns its content in strings, and content is passed to the write methods as strings. • File iterators are best for reading lines • Content is strings, not objects: data read from a file always comes back to your script as a string, so you’ll have to convert it to a different type of Python object if a string is not what you need.
  • 7. • The following code begins by opening a new text file for output, writing two lines (strings terminated with a newline marker, n), and closing the file.
  • 8. • If you want to display the file’s content with end-of-line characters interpreted, read the entire file into a string all at once with the file object’s read method and print it:
  • 9. Storing Python Objects in Files: Conversions • convert objects to strings using conversion tools. Again, file data is always strings in our scripts, and write methods do not do any automatic to-string formatting for us
  • 10. • the interactive echo gives the exact byte contents, while the print operation interprets embedded end-of-line characters to render a more user-friendly display: We now have to use other conversion tools to translate from the strings in the text file to real Python objects. As Python never converts strings to numbers (or other types of objects) automatically, this is required if we need to gain access to normal object tools like indexing, addition, and so on:
  • 11. For this first line, we used the string rstrip method to get rid of the trailing end-of-line character; a line[: 1] − slice would work, too, but only if we can be sure all lines end in the n character (the last line in a file sometimes does not). We still must convert from strings to integers, though, if we wish to perform math on these:
  • 12. int translates a string of digits into an integer object. Notice that we didn’t have to run rstrip to delete the n at the end of the last part; int and some other converters quietly ignore whitespace around digits. Finally, to convert the stored list and dictionary in the third line of the file, we can run them through eval, a built-in function that treats a string as a piece of executable program code
  • 14. Storing Native Python Objects: pickle • If you really want to store native Python objects, but you can’t trust the source of the data in the file, Python’s standard library pickle module is ideal. • The pickle module is a more advanced tool that allows us to store almost any Python object in a file directly, with no to- or from-string conversion requirement on our part. It’s like a super- general data formatting and parsing utility. • To store a dictionary in a file, for instance, we pickle it directly: Then, to get the dictionary back later, we simply use pickle again to re-create it:

Editor's Notes

  • #13: Using eval to convert from strings to objects, as demonstrated in the preceding code, is a powerful tool. In fact, sometimes it’s too powerful. eval will happily run any Python expression—even one that might delete all the files on your computer, given the necessary permissions!
  • #14: Using eval to convert from strings to objects, as demonstrated in the preceding code, is a powerful tool. In fact, sometimes it’s too powerful. eval will happily run any Python expression—even one that might delete all the files on your computer, given the necessary permissions!