This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Just completed this week's work, everything moving along well. One of the extra credits for exercise 17 has given me some trouble though, specifically #3:
"See how short you can make the script. I could make this 1 line long."
I was able to shorten some of the lines, e.g.:
#input = open(from_file)
#indata = input.read()
#combine to...
indata = open(from_file).read()
#output = open(to_file, 'w')
#output.write(indata)
#combine to...
open(to_file, 'w').write(indata)
Some of the prints could be combined as well.
Questions...
1. What other lines can be shortened, esp. from the beginning?
2. I had to remove the file "close" commands when combining the lines. Do the files remain open when opened inside the other command? Changing the last lines to close(from_file) and close(to_file) gives the error "NameError: name 'close' is not defined".
Thanks for the help,
PB
1. I believe the exercise is asking you to write the shortest possible program that copies the contents of one file to another. So I omitted all of the print statements entirely since they don't contribute to that functionality.
With that caveat, it's at the very least possible to get it to 2 lines; I had one line for the import statement and another for everything else. You can also make any number of lines all one line (technically) by using a semicolon in lieu of a line break, but that's basically cheating :). I'd be interested in seeing how far other people got with this so I won't post my version yet.
2. My "one-line" version also had no close statements. Using a close statement is what we call "best practice"- using it frees up memory on your machine when you close it, since python doesn't have to keep the file in active memory once it's no longer needed. Additionally, if you don't manually close the file you can sometimes see unexpected behavior. However, for this particular file it seems to work just fine without it. Since the point of this exercise was to make the shortest possible working version, not the best version, it's fine to leave it out.
Mind sharing all your one line versions? I didn't do that extra credit.
by the way, to avoid the file.close() statement you can do this:
the with statement together with the usage of file will handle the auto close of the files. If you didn't do it this way then u have to close the file in the "finally" clause. After all you need to close the file regardless of successful or failure open or writing.
try:
with open('something.txt') as the_file:
print thefile.readline()
except:
# print IO error or whatever.
Ok, here are my two lines:
from sys import argv
open(argv[2],'w').write(open(argv[1]).read())
Most of how you get to that is simply be substituting variables by their definition, just like one would do with math. If you wanted to solve y=2x and you had the definition x=3, you would just put "3" in the equation for y=2(3). This is the same principle.
Patrick got us started, showing how he combined statements to make
indata = open(from_file).read()
and
open(to_file, 'w').write(indata)
You can further combine those two to
open(to_file, 'w').write(open(from_file).read())
The last step is something that you'll be formally taught later. It's actually not necessary to "unpack" argv; you can access the elements in argv by index (position), since argv is a list. You can look up lists now or wait until we cover that in exercise 32.