Pages Navigation Menu

"It's not about the destination, it's about the journey. "

Learn Python The Hard Way, Exercise 17

I’m currently working/studying exercise 17 from Learn Python the Hard Way. In this exercise I learned two new things, and those two reasons are why I created this new category which will cater for my programming experiences, as opposed to me creating hundreds of pages and having my navigation bar up-top filled with drop down menus on the “code” navigation link.

As for my programming abilities so far, I’ve been typing out every single piece of code in this eBook by hand (As requested by the author) with no copy and pasting whatsoever! I’m quite proud of that actually.

Anyways, in the exercise it states that I need to condense the following code:

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?
in_file = open(from_file)
indata = in_file.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()

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

print "Alright, all done."

out_file.close()

To this:

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); in_file = open(from_file).read(); print "The input file is %d bytes long" % len(in_file); print "Does the output file exist? %r" % exists(to_file); print "Ready, hit RETURN to continue, CTRL-C to abort."; raw_input(); out_file = open(to_file, 'w').write(in_file); print "Alright, all done."

# we could do these two on one line too, how?
# indata = in_file.read()
# out_file.write(in_file)
# out_file.close()
# in_file.close()

As you can see I condensed the code to just one line with semi-colons, and I hashed out the code that wasn’t needed for the operation that the code is to do. It still works and copies from one file to the other.

I got rid of close() as I’m still not sure yet what purpose that serves in relation to this, though I’m assuming I can just add this to the end; though it’s throwing up errors. I’m just looking to progress onward and if I run into an issue like that I’ll have a sit down and think about solving it or asking on websites designed for these types of issues.

I also combined from the original code in line 18 and 19 to this:

out_file = open(to_file, ‘w’).write(in_file)

Not sure if that is correct; however, the script is still functioning and does its’ job.

I’m sure if an expert Python programmer came and saw this they would faint, but I’m still trying to learn and I know I have a long road ahead of me.

 New Parameters & Formatters

import  exists – This will confirm whether a file exists or not. If a file exists it will report back with True, if the file doesn’t exist it will report back with False. In Windows Powershell, it outputs the following:

Does the out-put file exist? True 

len() – (length) This will measure the length of a file. In the above example the output in Windows Powershell is:

The input file is 686 bytes long

Leave a Comment

Your email address will not be published. Required fields are marked *