SlideShare a Scribd company logo
XII. FILE PROCESSING
Engr. Ranel O. Padon
PYTHON PROGRAMMING TOPICS
I

• Introduction to Python Programming

II

• Python Basics

III

• Controlling the Program Flow

IV

• Program Components: Functions, Classes, Packages, and Modules

V

• Sequences (List and Tuples), and Dictionaries

VI

• Object-Based Programming: Classes and Objects

VII

• Customizing Classes and Operator Overloading

VIII

• Object-Oriented Programming: Inheritance and Polymorphism

IX

• Randomization Algorithms

X

• Exception Handling and Assertions

XI

• String Manipulation and Regular Expressions

XII

• File Handling and Processing

XIII

• GUI Programming Using Tkinter
Data Hierarchy

File
Processing

File-Open Modes
Dissecting Files
The Power of Buffering
Python Programming - XII. File Processing
FILE HANDLING

variables offer only temporary storage of data
they are lost when they “goes out of scope” or
when the program terminates
FILE HANDLING

files are used for long-term retention of
large amounts of data, even after the program
that created the data terminates.
data maintained in files is called persistent data
FILE HANDLING | Data Hierarchy

Bit (“Binary digit”) => the smallest computer data item
Bit is a digit that can assume one of two values
FILE HANDLING | Data Hierarchy

Programming with low-level bit formats is tedious & boring.
use decimal digits, letters, and symbols instead.
FILE HANDLING | Data Hierarchy

Characters are made-up of digits, letters, and characters.
Characters are represented as combination of bits (bytes).
FILE HANDLING | Data Hierarchy
FILE HANDLING | Data Hierarchy
Field (Column) is a collection of characters,
represented as words.
Record (Row) is a collection of fields,
represented as a tuple, dictionary, instance of a class.
File (Table) is a collection of records,
implemented as sequential access or random-access.
Database (Folder) is a collection of files,
handled by DBMS softwares.
FILE HANDLING | Data Hierarchy
FILE HANDLING | Data Hierarchy
FILE HANDLING | open() & close()
magical_file = open(“file_name.txt” [, a|r|r+|w|w+] [, buffer_mode])

magical_file.close()
FILE HANDLING | Other Functions
FILE HANDLING | open()

Open Mode

r
r+
w
w+
a
a+

Read

Write

Appends

Overwrites

Creates

Cursor @
Start

Cursor @
EOF
FILE HANDLING | Common Modes

Open Mode

r
w

Read

Write

Appends

Overwrites

Creates

Cursor @
Start

Cursor @
EOF
FILE HANDLING | open()
“r” is the default file-open mode
open(“input.dat”) = open(“input.dat”, “r”)
FILE HANDLING | r
FILE HANDLING | r
FILE HANDLING | r
FILE HANDLING | w

 try removing line #6
 try removing "n" in lines #3 and #4
FILE HANDLING | w
FILE HANDLING | with-as Keyword
FILE HANDLING | Parsing

Paninda.txt
FILE HANDLING | Parsing | split
FILE HANDLING | Parsing | split
FILE HANDLING | Parsing | csv

Paranormal_Sightings.csv
FILE HANDLING | Parsing | strip
FILE HANDLING | Parsing | strip
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | Parsing & Classes 2
FILE HANDLING | HTML Parsing
MangJose.html
FILE HANDLING | HTML Parsing
MangJose.html
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | HTML Parsing
FILE HANDLING | r+, w+, a+
All of the "plus" modes allow reading and writing:
the main difference between them is where
we're positioned in the file.

“r+” puts us at the beginning
“w+” puts us at the beginning & the end,
because the file's truncated
“a+” puts us at the end.
FILE HANDLING | w+
FILE HANDLING | Buffering
FILE HANDLING | Buffering
“-1” is the default file-open buffering mode
open(“input.dat”) = open(“input.dat”, “r”, “-1”)
Flag

Meaning

0

unbuffered

1

buffered line

n

buffered with size n

-1

system default
FILE HANDLING | Creating A Big File!
FILE HANDLING | Unbuffered r
Then, let’s read that big file. 
FILE HANDLING | Buffered r
Now, with the help of buffering. 
FILE HANDLING | Buffered By Default

In other languages, like C or Java,
buffering is not the default mode.
FILE HANDLING | What else?
1. Random-Access Files: for fast searching/editing of records
* use the shelve module
* shelve.open()
2. Serialization: compressing file as objects for efficiency;
useful for transferring data (objects, sequences, etc)
across a network connection or saving states of a game
* use the pickle or cPickle module
* cPickle.dump(stringList_to_be_written, serialized_file)
* records = cPickle.load(serialized_file)
Python Programming - XII. File Processing
PRACTICE EXERCISE| MORSE CODE
PRACTICE EXERCISE| MC CHART
PRACTICE EXERCISE| MC CHART
PRACTICE EXERCISE| MORSE CODE

A. Read a file containing Filipino/English-language
phrases and encodes it into Morse code.
B. Read a Morse code file and converts it into the
Filipino/English-language equivalent.

Use one blank between each Morse-coded letter and
three blanks between each Morse-coded word.
REFERENCES
 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/information used here have no proper source
citation, and I do not claim ownership of these either. I don’t want to reinvent the
wheel, and I just want to reuse and reintegrate materials that I think are useful or
cool, then present them in another light, form, or perspective. Moreover, the
images/information here are mainly used for illustration/educational purposes only,
in the spirit of openness of data, spreading light, and empowering people with
knowledge. 

More Related Content

What's hot (20)

Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Java8
Java8Java8
Java8
Felipe Mamud
 
Python second ppt
Python second pptPython second ppt
Python second ppt
RaginiJain21
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
MOHIT DADU
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
RojaPriya
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Prof. Dr. K. Adisesha
 
Python
PythonPython
Python
Rural Engineering College Hulkoti
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Codemotion
 
What is Python?
What is Python?What is Python?
What is Python?
wesley chun
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
Python and You Series
Python and You SeriesPython and You Series
Python and You Series
Karthik Prakash
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
Akhil Kaushik
 
Data file handling
Data file handlingData file handling
Data file handling
Prof. Dr. K. Adisesha
 
Function overloading
Function overloadingFunction overloading
Function overloading
Prof. Dr. K. Adisesha
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
Akanksha Bali
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Maulik Borsaniya
 
Pointers
PointersPointers
Pointers
Prof. Dr. K. Adisesha
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
zynofustechnology
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
Richard Thomson
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
MOHIT DADU
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
RojaPriya
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Codemotion
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
Akanksha Bali
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Maulik Borsaniya
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
zynofustechnology
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
Richard Thomson
 

Viewers also liked (12)

Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Ad

Similar to Python Programming - XII. File Processing (20)

Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
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...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
 
Python-files
Python-filesPython-files
Python-files
Krishna Nanda
 
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
4HG19EC010HARSHITHAH
 
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
qover
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
ARYAN552812
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
Rohan Gajre
 
File management
File managementFile management
File management
sumathiv9
 
File handing in C
File handing in CFile handing in C
File handing in C
shrishcg
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
SanjayKumarMahto1
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
Python Training
Python TrainingPython Training
Python Training
softcrayons3
 
File Handling in Python -binary files.pptx
File Handling in Python -binary files.pptxFile Handling in Python -binary files.pptx
File Handling in Python -binary files.pptx
deepa63690
 
file handling.pdf
file handling.pdffile handling.pdf
file handling.pdf
RonitVaskar2
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
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...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
 
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
qover
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
Rohan Gajre
 
File management
File managementFile management
File management
sumathiv9
 
File handing in C
File handing in CFile handing in C
File handing in C
shrishcg
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
SanjayKumarMahto1
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
File Handling in Python -binary files.pptx
File Handling in Python -binary files.pptxFile Handling in Python -binary files.pptx
File Handling in Python -binary files.pptx
deepa63690
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Ad

More from Ranel Padon (8)

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 

Recently uploaded (20)

Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 

Python Programming - XII. File Processing

  • 1. XII. FILE PROCESSING Engr. Ranel O. Padon
  • 2. PYTHON PROGRAMMING TOPICS I • Introduction to Python Programming II • Python Basics III • Controlling the Program Flow IV • Program Components: Functions, Classes, Packages, and Modules V • Sequences (List and Tuples), and Dictionaries VI • Object-Based Programming: Classes and Objects VII • Customizing Classes and Operator Overloading VIII • Object-Oriented Programming: Inheritance and Polymorphism IX • Randomization Algorithms X • Exception Handling and Assertions XI • String Manipulation and Regular Expressions XII • File Handling and Processing XIII • GUI Programming Using Tkinter
  • 5. FILE HANDLING variables offer only temporary storage of data they are lost when they “goes out of scope” or when the program terminates
  • 6. FILE HANDLING files are used for long-term retention of large amounts of data, even after the program that created the data terminates. data maintained in files is called persistent data
  • 7. FILE HANDLING | Data Hierarchy Bit (“Binary digit”) => the smallest computer data item Bit is a digit that can assume one of two values
  • 8. FILE HANDLING | Data Hierarchy Programming with low-level bit formats is tedious & boring. use decimal digits, letters, and symbols instead.
  • 9. FILE HANDLING | Data Hierarchy Characters are made-up of digits, letters, and characters. Characters are represented as combination of bits (bytes).
  • 10. FILE HANDLING | Data Hierarchy
  • 11. FILE HANDLING | Data Hierarchy Field (Column) is a collection of characters, represented as words. Record (Row) is a collection of fields, represented as a tuple, dictionary, instance of a class. File (Table) is a collection of records, implemented as sequential access or random-access. Database (Folder) is a collection of files, handled by DBMS softwares.
  • 12. FILE HANDLING | Data Hierarchy
  • 13. FILE HANDLING | Data Hierarchy
  • 14. FILE HANDLING | open() & close() magical_file = open(“file_name.txt” [, a|r|r+|w|w+] [, buffer_mode]) magical_file.close()
  • 15. FILE HANDLING | Other Functions
  • 16. FILE HANDLING | open() Open Mode r r+ w w+ a a+ Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  • 17. FILE HANDLING | Common Modes Open Mode r w Read Write Appends Overwrites Creates Cursor @ Start Cursor @ EOF
  • 18. FILE HANDLING | open() “r” is the default file-open mode open(“input.dat”) = open(“input.dat”, “r”)
  • 22. FILE HANDLING | w  try removing line #6  try removing "n" in lines #3 and #4
  • 24. FILE HANDLING | with-as Keyword
  • 25. FILE HANDLING | Parsing Paninda.txt
  • 26. FILE HANDLING | Parsing | split
  • 27. FILE HANDLING | Parsing | split
  • 28. FILE HANDLING | Parsing | csv Paranormal_Sightings.csv
  • 29. FILE HANDLING | Parsing | strip
  • 30. FILE HANDLING | Parsing | strip
  • 31. FILE HANDLING | Parsing & Classes
  • 32. FILE HANDLING | Parsing & Classes
  • 33. FILE HANDLING | Parsing & Classes
  • 34. FILE HANDLING | Parsing & Classes 2
  • 35. FILE HANDLING | Parsing & Classes 2
  • 36. FILE HANDLING | Parsing & Classes 2
  • 37. FILE HANDLING | Parsing & Classes 2
  • 38. FILE HANDLING | HTML Parsing MangJose.html
  • 39. FILE HANDLING | HTML Parsing MangJose.html
  • 40. FILE HANDLING | HTML Parsing
  • 41. FILE HANDLING | HTML Parsing
  • 42. FILE HANDLING | HTML Parsing
  • 43. FILE HANDLING | HTML Parsing
  • 44. FILE HANDLING | HTML Parsing
  • 45. FILE HANDLING | HTML Parsing
  • 46. FILE HANDLING | r+, w+, a+ All of the "plus" modes allow reading and writing: the main difference between them is where we're positioned in the file. “r+” puts us at the beginning “w+” puts us at the beginning & the end, because the file's truncated “a+” puts us at the end.
  • 48. FILE HANDLING | Buffering
  • 49. FILE HANDLING | Buffering “-1” is the default file-open buffering mode open(“input.dat”) = open(“input.dat”, “r”, “-1”) Flag Meaning 0 unbuffered 1 buffered line n buffered with size n -1 system default
  • 50. FILE HANDLING | Creating A Big File!
  • 51. FILE HANDLING | Unbuffered r Then, let’s read that big file. 
  • 52. FILE HANDLING | Buffered r Now, with the help of buffering. 
  • 53. FILE HANDLING | Buffered By Default In other languages, like C or Java, buffering is not the default mode.
  • 54. FILE HANDLING | What else? 1. Random-Access Files: for fast searching/editing of records * use the shelve module * shelve.open() 2. Serialization: compressing file as objects for efficiency; useful for transferring data (objects, sequences, etc) across a network connection or saving states of a game * use the pickle or cPickle module * cPickle.dump(stringList_to_be_written, serialized_file) * records = cPickle.load(serialized_file)
  • 59. PRACTICE EXERCISE| MORSE CODE A. Read a file containing Filipino/English-language phrases and encodes it into Morse code. B. Read a Morse code file and converts it into the Filipino/English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.
  • 60. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 