Hello, and welcome to the reality show about exercises in Python.
It's not a very exciting reality show, but I'm your host, Charles Severance.
And these exercises come from Python for Informatics: Exploring Information.
And, of course this material's Creative Commons Attribution,
as well as the book and other materials.
So by now I hope that you're getting pretty good at using Python.
Hopefully the pythonlearn materials have been helpful to get things set up.
So the exercise that we're working on today is
Exercise 7.3 from the Files chapter, and our goal here is
is simply to read through a file and print
out the contents of a file, all in upper case.
Executing the program will look like this screen.
We obviously have to prompt for the file
name, because it says enter the file name and
then we give it a file name, or we
get our file from this URL shown here, py4inf.com/code/mbox-short.txt.
So let's do that.
And so let's, let's go ahead and do this.
So the first thing we're going to do is
go to the web, and here's Python for Informatics.
By the time you're seeing this maybe it'll be even prettier.
And the URL is in this code samples.
And this is old school user look and feel.
And I'm going to open this, and this is the data that we're going to work with.
The mailbox format data.
And I'm going to save this as a text file.
And I'm going to put it in my desktop.
In a folder Python for Informatics and I'm going to put this mailbox file
right in the same place as all of my other files.
I'm going to verify that that's looking really good now by opening
up a Terminal window, and I go into that folder.
And that, that folder right there is in my desktop.
Python.
And there we are. I got my file.
Okay?
So I'll open up TextWrangler.
Make it a little smaller.
Okay.
And then I could open this file in TextWrangler if I wanted to.
mbox-short, and so there it is.
If I want to, like, look at it really closely.
Or we could open, I don't want that.
I want to make a new Python program.
And so we will, you know, well, the first thing
we gotta do is, we gotta enter a file name.
So let's do that.
I'll call my variable fname equals
raw_input, enter a file name,
colon, insert a space, oops.
Switch back to single quotes.
You could use double quotes or single quotes but single quotes are cool.
Double quotes make it look like you're a Java programer
that can't remember whether it's single quotes or double quotes.
Python doesn't care about it, but the cool people use single quotes.
So use cool.
I mean if you want to be cool.
I'm trying to be cool.
You can see I sometimes don't get it at all.
I'm not that cool.
Okay.
So then I want to print file name, and let's save this,
and then as soon as we save it, you'll see that, python for informatics,
I'm going to call the program shout.py, and as
soon as we get it, we get nice syntax
coloring, which helps us understand if we've made any,
like, typographical error like that, or something like that so.
So we'll save it and if I come here right now I'll see
shout.python, shout.py, and I say python shout.py.
Enter file name, fred. Looks good.
OK? I didn't do much else.
So, the thing we've gotta do, of course,
to read a file, is we can't just read it.
We're going to have make them get a file name, right?
So we'll call our file handle fhand variable equals open
and then fname is the file.
And then, of course, we can write a for loop for line in, now line is just
a variable name, for and in are keywords,
but line is just a variable name, and fhand.
And that's because the file handle, file handle is a type of sequence that we can
iterate through in a for loop, and this loop will run for each successive
line in the file.
And so I will just say print line to kind
of know that my loop is actually iterating through the file.
So we've got four lines, and we'll say fred again.
And son of a gun if the open kind of blows up, because there is no fred.
So we have to run a program here that's actually in the directory.
mbox-short.
I've shortened the long one, so that mbox-short, is
just a, you know, it's a little easier, smaller.
It's just the first few lines of the full mbox file.
Okay so here we go, and, there we are, there we are,
and I'm going to have to make that a little bit bigger.
So if we go up here and we look at this
one of the things that we see is that it doesn't look
like the file because there is a blank line after every line.
And you say oh crap, why is that?
Well, you're just going to have to get used to the idiom that is
trimming lines, and so I'm going to, I'm going to trim.
I'm only going to trim on the right side.
Line.rtrim.
I'm going to trim the white space on the end
and the problem is, is there's a
newline here, and that print statement is also adding a newline.
So I'm just going to trim it.
[BLANK_AUDIO]
Save that.
[BLANK_AUDIO]
Make it smaller so we can kind of see it going at the same time.
I use Cmd+K on my MacIntosh to clear the screen.
You can actually use cls in Windows if you want.
And so I'm going to run the same program again.
mbox-short.txt, and now we should.
Oh, oh, oh, it's not rtrim.
Object, str object has no attributes rtrim, because it's rtrim
in some other language, and I'm forgetting, because I'm switching
between languages, but I just, there's a no method, and
you'll notice that the syntax coloring is not right either.
And it's rstrip.
How many people caught that?
Not many, probably.
I didn't even catch it.
Let's try it again.
mbox-short.txt.
Okay, so now it's still kind of ugly and nasty,
but you can kind of see there's, when we print
the file out here, there's no extra blank lines
and that's because the newlines have been stripped off.
We're still getting a newline from the print statement, but we only want one newline.
Okay, so we're getting pretty close.
We've got, in this little loop right here, we've
got the line, we've taken off the white space.
So now it's a pretty simple task to just say line equals line.upper,
and then we're going to convert the whole thing to uppercase and run it again.
mbox-short.txt
There we go.
It is shouting, shouting, shouting, because everything is in upper case.
So that's pretty straightforward, not too much error checking.
Let me show you one, one sort of Python idiom that allows you to contract this.
So in a sense, this is a function call.
It says, give me back a new string, which
is a copy of line with the white space removed.
Well, this is a string, and we can apply a string library method to it, as well.
So I can actually tack on the end of this the upper, and I can take this line out.
And now I can do it all in one line.
And this code says, take line, strip the white space, and then take
the resulting white space free, and convert it to upper case.
And that should accomplish the same thing.
Let's make sure
we don't have blank lines.
We don't have blank lines.
So everything is fine.
Okay?
So we could add a few things.
Let's add something to this.
We've got their specifications done.
We're kind of done at the minimum, but let me show you something cool here.
Let's just say you got tired of typing that file name all the time.
Here's a little trick.
If the length of fname
is less than one.
I'll just say equals zero.
You're going to set the fname, fname
equals mbox-short.txt.
So that basically means that if we enter a real file
name, the length of the file name will be greater than zero.
If we don't, we're just going to assume we're just going to change that
variable to fname, and then when we open it, we'll have it.
So watch this.
Oh, better save that.
[BLANK_AUDIO]
So I could type a file name but if I hit Enter, it just does that.
Look how convenient that is.
So, in effect it was creating a default, as it were.
When the user types enter, they sort of do their thing.
Okay?
So, that's pretty much it for the Exercise 7.3.
The shouting at exercise.