Reading Files
Python Programming
April 1, 2022
Source: <Python - Reading Files>
STEAM Clown ™ Productions
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Licensing & Attribution
My Open Source Philosophy - I have come to realize that I’m really not competing with other teachers. Maybe I have some local competition, �but in reality, if a student is not coming to my class or my school, then I’m not competing with other teachers in any way… If you work more than a �District away, then we really are not competitors. Let's Share... Collaborate... Help each other... I, and you, may have sweat blood and tears developing a lesson plan, a lab, or presentation… There is an ENORMOUS unpaid value there… I get that… I don’t begrudge teachers who are trying to sell their collateral… I’m just saying that is not what I’m going to do. This is my plan… I’m going to share virtually everything I develop. Join Me!!! See More
CC BY-NC-SA 4.0
https://creativecommons.org/licenses/by-nc-sa/4.0/
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
GNU Public License & EUPL (European Union Public Licence)
Any included or linked Programming Code Is licensed under GNU General Public License v3.0 & and / or licensed under EUPL 1.2 or later
See the Appendix for Additional Licensing & Attribution Information
2
Please maintain this slide with any modifications you make:
This content is licensed as Open Source / Creative Commons: A Specific use license has been provided to the Silicon Valley education Foundation & Silicon Valley CTE
Much of the content in these slides is a Re-Telling of material provided by Dr. Chuck Severance - From PY4E, or adapted from the book Automate the Boring Stuff with Python. Both are Awesome open source sites for teaching Python. I have modified them some, to work better for my Mechatronics Engineering class.
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Objective, Overview, & Introduction
Variables are a fine way to store data while your program is running, but if you want your data to persist even after your program has finished, you need to save it to a file
3
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
When You See:👉 Try This:
4
print("Hisssssssssss")
Try This 👉
Try This
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Sponsorship Page
I would like to thank <Your Company or Organization Here> for their generous support of my classroom and curriculum development. <Your Company or Organization Here> has <Your value proposition, call to action, & related content and message>
How to Provide a Stipend: You can send something to my personal PayPal.Me Account, Venmo Account, or my Patrion Account, even Buy Stuff On My Amazon Classroom Wishlist, donate to my DonorsChoose projects, or contact me at TopClown@STEAMClown.org, if you want to contribute another way.
(If you would like to sponsor this presentation, please contact TopClown@STEAMClown.org )
5
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What You Will Know...
Prior Knowledge & Certifications
What You Will Know & Be Able To Do
6
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
How You Will Be Measured
7
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Essential Questions
8
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Resources & Materials Needed
9
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
New Words or Concepts...
10
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Prerequisite or Pre-Quiz...
This is not a graded Quiz… It’s to help me and you understand what you already know, and where you may need to focus...
11
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Mess With Data - Where Is It?
12
Software
Ram �Main Memory
Central Processing Unit
Secondary Memory
Input / Output
I'm Talking Here
if x<3:
print('less')
What's Next?
Files R Us
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What Is A File?
A text file can be thought of as a sequence of lines
13
2021-04-23,09:26:05pm,robot2,led-2,on
2021-04-23,09:34:55pm,robot2,servo,wave
2021-04-23,10:38:10pm,robot2,rgb-led,green
2021-04-23,10:39:05pm,robot2,led-2,off
2021-04-23,11:41:55pm,robot2,servo,wave
2021-04-23,11:42:02pm,robot2,led-1,on
2021-04-24,01:11:02am,robot2,led-1,off
2021-04-24,01:41:55am,robot2,servo,90
This one has lines that are a list of comma delimited strings
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What Is A File?
A text file can be thought of as a sequence of lines
14
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: source@collab.sakaiproject.org
From: stephen.marquard@uct.ac.za
Subject: [sakai] svn commit: r39772 - content/branches/
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
This one looks like an email message
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What Is A File?
A text file can be thought of as a sequence of lines
15
This is your last chance.
After this, there is no turning back.
You take the blue pill - the story ends,
you wake up in your bed and believe
whatever you want to believe.
You take the red pill - you stay in
Wonderland and I show you how deep
the rabbit-hole goes.
Take the Red Pill
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening a File
16
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Copy or make a text file
If you want to download a file using the raspberry Pi command terminal you can use this command
wget http://path_to_file�wget -O new_file_name http://path_to_file ← Over Writes
You should cd to the directory where you want this file to get copied to. The wget will copy the file to the current working directory.
17
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Using open()
18
The function open() returns a handle use to manipulate the file
filename is a string
mode is optional and should be 'r' if we are planning to read the file and 'w' if we are going to write to the file
handle = open(filename, mode)
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Using open()
19
handle = open(filename, mode)
fhand = open('matrix.txt', 'r')
returns the file handle pointer to the file
name of file
mode to open as read only
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Using open() To Get The File Handle
20
fhand = open('matrix.txt', 'r')
print(fhand)
<_io.TextIOWrapper name='matrix.txt' mode='r' encoding='UTF-8'>
matrix.txt
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
When Files are Missing
21
fhand = open('stuff.txt', 'r')
print(fhand)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'stuff.txt'
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
The newline Character
22
stuff = 'Hello\nWorld!'
print(stuff)
stuff = 'X\nY'
print(stuff)
len(stuff)
Hello
World!
X
Y
3
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
File Processing
A text file can be thought of as a sequence of lines
23
This is your last chance.
After this, there is no turning back.
You take the blue pill - the story ends,
you wake up in your bed and believe
whatever you want to believe.
You take the red pill - you stay in
Wonderland and I show you how deep
the rabbit-hole goes.
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
File Processing
A text file can be thought of as a sequence of lines�A text file has newlines at the end of each line
24
This is your last chance.\n
After this, there is no turning back.\n
\n
You take the blue pill - the story ends,\n
you wake up in your bed and believe\n
whatever you want to believe.\n
You take the red pill - you stay in\n
Wonderland and I show you how deep\n
the rabbit-hole goes.\n
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Reading Files in Python
25
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
File Handle as a Sequence
26
xfile = open('matrix.txt')
for cheese in xfile:
print(cheese)
This is your last chance.
After this, there is no turning back.
You take the blue pill - the story ends,
you wake up in your bed and believe
whatever you want to believe.
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
A Tale Of Two \n
Remember that the file has \n characters at the end of every line...
27
xfile = open('matrix.txt')
for cheese in xfile:
print(cheese)
This is your last chance.\n
After this, there is no turning back.\n
\n
You take the blue pill - the story ends,\n
you wake up in your bed and believe\n
whatever you want to believe.\n
You take the red pill - you stay in\n
Wonderland and I show you how deep\n
the rabbit-hole goes.\n
This is your last chance.
After this, there is no turning back.
You take the blue pill - the story ends,
you wake up in your bed and believe
whatever you want to believe.
This is your last chance.\n
\n
After this, there is no turning back.\n
\n
\n
\n
You take the blue pill - the story ends,\n
\n
you wake up in your bed and believe\n
\n
whatever you want to believe.\n
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening a file with a File Handle
28
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening a file with a File Handle
29
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Counting Lines in a File
30
fhand = open('matrix.txt')
# Need a variable to count the lines
for line in fhand:
# Need to increment the line count Variable
print(line)
print('Line Count:', count)
fhand = open('mbox-short.txt')
# Need a variable to count the lines
for line in fhand:
# Need to increment the line count Variable
print(line)
print('Line Count:', count)
Line Count: 10
Line Count: 191
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening a file and Reading Line by Line
& Counting lines
31
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Striping Characters from a line in a File
32
xfile = open('mbox-short.txt')
for line in xfile:
print(line)
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
...
What are all these blank lines doing here?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Striping Characters from a line in a File
33
xfile = open('mbox-short.txt')
for line in xfile:
if line.startswith('From:'):
print(line)
From: stephen.marquard@uct.ac.za\n
\n
From: louis@media.berkeley.edu\n
\n
From: zqian@umich.edu\n
...
Remember the \n
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lots of String Methods
34
Let's Try This One
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
.rstrip()
35
xfile = open('mbox-short-short.txt')
for line in xfile:
line = line.rstrip()
print(line)
From: stephen.marquard@uct.ac.za\n
From: louis@media.berkeley.edu\n
From: zqian@umich.edu\n
...
The \n is from the print()
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening a file and Reading Line by Line
& Counting lines
36
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening and reading the "Whole" file
37
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Reading the *Whole* File
38
xfile = open('matrix.txt')
whale = xfile.read()
print(len(whale))
print(whale[80:89])
273
blue pill
whale is a big string that can be indexed
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening and reading the "Whole" file
39
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Searching Through a File
40
xfile = open('mbox-short.txt')
for line in xfile:
if line.startswith('From:'):
print(line)
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
...
What are all these blank lines doing here?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Searching Through a File
41
xfile = open('mbox-short.txt')
for line in xfile:
if line.startswith('From:'):
print(line)
From: stephen.marquard@uct.ac.za\n
\n
From: louis@media.berkeley.edu\n
\n
From: zqian@umich.edu\n
...
Remember the \n
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening, reading, and finding stuff in a file
42
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening, reading, and making a list of emails
43
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Opening and Reading "Robot" Commands
44
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Summary
Dig Deeper:
45
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Thank You…
Questions?
46
Jim Burnham (AKA STEAM Clown)
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Reference Slides
47
STEAM Clown ™ Productions
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Links for Students
48
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Teachers: Before You Present
49
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Key: 📰 Slides / Audio 🎧 / 📽️▶️ Video/YouTube / 🎧▶️📽️ Audio/Video / ✨ Resources / 🖼️ Tutorial / 📖 Reading Activity / 📝 Writing Activity / 📖 📝 Reading/Writing / 📟 Coding / 🛠️ LAB Activity / 🚀 Quiz / 🔎 Review / ✔️ Mastery Check / ✍️ Sign Up /🍕 Extra Credit / 🕸️ Web Links / 👩🏽🎓🧑🏽🎓🧑🏿🎓👩🏫 Class / 🏵️📜📃 Certificate
/🧟 Review / 🦾 Practice / 🆙Level Up / 🎚️🦑📤🎯 🚧 - 🦑 Special Project -
Assignment Type: ⚓ Establishing (Minimum Standard) / ⛏️ Developing (Digging Deeper) / 💎 Aspiring (Putting It Together)
This is an ⚓ Establishing Assignment (Minimum Standard) - "Everyone Do" Assignment
This is an ⛏️ Developing (Digging Deeper) - "Everyone Should Do, To Stretch" Assignment
This is an 💎 Aspiring (Putting It Together) - "When you have done the ⚓ Establishing and⛏️ Developing" Assignment
50
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Education Standards
51
51
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
52
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Appendix
53
STEAM Clown ™ Productions
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Appendix : Primary Sources & Attribution for Material Used
54
Please maintain this slide with any modifications you make
Much of the content in these slides is a Re-Telling of material provided by Dr. Chuck Severance - From PY4E, or adapted from the book Automate the Boring Stuff with Python. Both are Awesome open source sites for teaching Python. I have modified them some, to work better for my Mechatronics Engineering class.
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Appendix : Primary Sources & Attribution for Material Used
55
Please maintain this slide with any modifications you make
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Image Reference & Sources
56
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
57
Please maintain this slide with any modifications you make
STEAM Clown™ - License & Attribution Creative Commons License CC-BY-NC-SA 4.0 - Much of this interpretation is primarily the Intellectual Property of Jim Burnham, TopClown, at STEAMClown.org, I may have used other sources, and in that Re-Telling, I have made my best attempt to properly attribute, or reference any of these sources or works. They are probably listed below. I’m a Super Huge proponent of Open Source - Find out more...
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What To Fix, Add, Or Change
Presentation Planning:
58
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions