views:

228

answers:

4

I'm doing Zed Shaw's fantastic Learn Python The Hard Way, but an extra question has me stumped: Line 9--10 could be written in one line, how? I've tried some different thoughts, but to no avail. I could move on, but what would the fun in that be?

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

# we could do these two on one line too, how?
input = open(from_file)
indata = input.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

output = open(to_file, 'w')
output.write(indata)

print "Alright, all done."

Zed also writes that he could do the whole script in one line. I'm not exactly sure what he means by that.

Feel free to help me however you want: by giving the answer or merely hinting---and perhaps including a collapsed or hidden answer to the question.

+7  A: 
indata = open(from_file).read()
GSto
This can also be done when writing.
Zonda333
yeah - though this (as well as the two-line solution) will leave the file open longer than needed, so you should avoid this in real code (a better way to do it in one line is `with open(from_file) as f: indata = f.read()`, if you want to save lines just for the hell of it)
delnan
This seems the closest to the Python been taught up until that exercise. I just don't see how anyone would be able to infer such a solution from a completely new background - I also know some Java, but would not have thought of something like that.Is there a special terminology for this specific measure?I can't figure out how someone would figure this out, but maybe the extra questions are just aimed at a higher level than I presumed.Regardless, thanks a bunch. I would probably not have figured it out without the explicit solution. The same applies to everyone else's help.
Kiwi
@Kiwi You would notice that `input` is set once and used once immediately after and therefore you can remove the temporary variable by using the expression itself.
mathepic
+5  A: 

shutil is the way to do one-liner file copies in Python:

shutil.copy(sys.argv[1], sys.argv[2])

Putting the import shutil, sys on the same line as this one (with a semicolon in-between, of course) would however be stylistically goofy;-).

Alex Martelli
This doesn't seem to address the question, though? Usually programming tutorials focus on concepts, so answers using different concepts seem less valuable to the student.
dash-tom-bang
"Less valuable" than _what_? `';'.join` of all the non-comment lines?-)
Alex Martelli
+2  A: 

Well you can just do "algebraic substitution," right? ...assuming you don't care about the "UI"...

open(to_file, 'w').write(open(from_file).read())
dash-tom-bang
remember `to_file = argv[2]`, etc!
PreludeAndFugue
Exercise left for the reader? :)
dash-tom-bang
A: 

I agree with the algebraic substitution mentioned by @dash-tom-bang. My functioning Exercise 17 extra credit has 5 lines. The operation is being conducted on one line.

open(to_file, 'w').write(open(from_file).read())

followed by a simple 'print' for verification feedback

print "File %s copied to %s" % (from_file, to_file)

I should have a 6th line that replaces the original ''output.close'' but I am confused about how to do this without the ''output'' variable? Ahh, since I now have no output variable there is nothing to close.

btw- It is a little spooky for me to see the same exact line typed here that I have worked out and entered myself in gedit. Great stuff, I am really enjoying the mental challenge and community support.

Edit:answered my own question