[Tutor] help with exercise 15 of zed shaw's LPTHW

Peter Otten __peter__ at web.de
Wed Jul 6 04:22:06 EDT 2016

lohecn at tuta.io wrote:

> hey everyone. this is my first time trying this -- actually, I've been
> studying python only for some days now, and I'm afraid my questions are
> going to be reeeeally simple, but I can't seem to understand this piece of
> code and thus can't move on.

You seem to be talking about


from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

As others said, always provide the code you are asking about, or if that is 
not possible at least provide a link.
> you probably know the book, so you know that zed always makes us write
> code so that then we can understand how it works, and it's great, but in
> this exercise there are just too many new functions and without
> explanation they are a bit hard to understand... so I'm having trouble
> with most of the lines here.
> it's not that I want the full explanation to that code, but since I'm
> unfamiliar with some of its concepts, I'm just going to tell you all the
> things that I don't understand (sorry for it being a lot):
> 1. the need to put script into an estipulation for argv (line 3)

Write a script tmp.py containing

from sys import argv
print argv

then call it with with one parameter, e. g.

$ python tmp.py somefile.txt
['tmp.py', 'somefile.txt']

As you can see argv is a list with two items, the first being "tmp.py", the 
name of the script you are invoking. You are only interested in the second 
one, the filename. The easy way to get that is

filename = argv[1]

the hard way is to use "unpacking"

script, filename = argv

where python will assign one item from the list on the right to every name 
on the left:

>>> items = ["foo", "bar"]
>>> one, two = items
>>> one
>>> two

What happens if the number of names on the left doesn't match the number of 
items in the list?

>>> one, two, three = items
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

You get an exception. That is why you have to provide the name "script" in 
Zed's example even though you are not actually interested in the script 

> 2. the what is txt and why it has to be used there (line 4)

txt is a file object and

> 3. txt.read() -- which are all new functions(? I dont even know what they
> are)  (line 7)

read() is a method that here reads the whole file into a string. You use the 
open() function to open a file and usually assign the file object that is 
returned by open to a name. You can freely choose that name. The structure 
is the same for every object, be it a number:

x = 42  # assign a number to x
y = x + x  # do some arithmetic with x and assign the result to y
print y  # print the result

a list:

mynumbers = list()  # create a list
mynumbers.append(42)  # append a number to the list
print mynumbers  # print the list

or a file:

myfile = open("example.txt")  # open the file example.txt in the current
                              # working directory. If the file doesn't exist
                              # you get an error

print "first line:", myfile.readline()  # read the first line and print it
print "rest of the file:"
print myfile.read()  # read the rest of the file and print it

myfile.close()  # close the file

> 4. file_again (line 10)
> 5. txt_again (line 12)
> and line 14.

4. and 5. are just a repetition of the first part, with the variation that 
the filename, assigned to file_again is read interactively with raw_input() 
instead of passing it as a commandline argument to the script.

The names used can be freely chosen by the programmer, a script

from sys import argv

red_apple, my_hat = argv

blue_suede_shoes = open(my_hat)
print blue_suede_shoes.read()

would work exactly like the first part of the hard-way example. However, 
picking descriptive names and using them consistently makes it much easier 
for a human reader to understand what's going on.

More information about the Tutor mailing list