On 06/07/16 15:04, loh...@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[1] print "Here's your file %r: " % filename print open(filename).read() filename = raw_input("Type the filename again: >") print open(filename).read() But your book is (I assume) trying to teach you good practice. 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 best choice. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor