[Tutor] help with exercise 15 of zed shaw's LPTHW
alan.gauld at yahoo.co.uk
Wed Jul 6 14:33:08 EDT 2016
On 06/07/16 15:04, lohecn at tuta.io wrote:
> 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()
> why do I have to create a variable txt_again to assign it to the open
> function and them print the file?
You don't, and could get away with a single
variable - filename. Like this:
filename = argv
print "Here's your file %r: " % filename
filename = raw_input("Type the filename again: >")
But your book is (I assume) trying to teach you
While you could have just printed the result of read directly,
its better not to over complicate code by doing too much in one
line (for a start, its harder to debug) the same variable.
Variables are cheap to create and if given useful names
tell us a lot about the purpose of the code.
In this case it's all a bit trivial, just printing the file
content, but if you were doing more complex processing
storing the file (and data) in variables would be the
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor