Learn Python The Hard Way, Exercise 17

by | Mar 14, 2013 | Updates

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

January 2020 – Running & Fitness

January 2020 – Running & Fitness

Originally created on January 1st, 2020: Updated daily until the end of January. I've decided that I will be keeping a log this year of all the runs that I've done this year by day and have a blog page for each month of the year. This blog post specifically will be...

Saint Espresso & Kitchen, Kentish Town

Saint Espresso & Kitchen, Kentish Town

Hanging around or about in Kentish Town? Want to go somewhere nice for brunch? Why not stop by over at Saint Espresso kitchen, just a 1 minute walk from Kentish Town underground station. The cafe is usually quite busy on the weekends and at around lunch time, so...

Hot Wok Kitchen, Archway

Hot Wok Kitchen, Archway

If you are looking for a peng place to eat, and that's close to Archway or Upper Holloway, then look no further than the restaurant that is Hot Wok Kitchen.It is a Japanese restaurant that serves excellent food and in fact, I'd say they are the best Japanese...

London Marathon 2015 – Training & Thoughts – 42.1km

London Marathon 2015 – Training & Thoughts – 42.1km

This is more of a retrospective post, looking at how I managed to do the London Marathon on April 26th, 2015. The London Marathon was my first, and currently only marathon that I've ever done. It holds a special milestone in my life, given all the issues I had gone...

What weight is a good weight to get back to?

What weight is a good weight to get back to?

As part of my plan/goal for 2020, I want to lose weight and get jacked up. I've gone through quite a journey, historically speaking, and given my sort of bumpy road of losing weight, then gaining weight, and then being content, and then not being content with my...

Becoming a Taiwanese national through descent…

Becoming a Taiwanese national through descent…

Originally posted Sep 6, 2011. Updated: Jan 3, 2020. Once Taiwanese you will be required to serve 12 months as a conscript in the Taiwanese military. Anyways, this blog post is essentially just the process you'll go through to get all my documents verified by two...

First run of 2020 – 5.89km

First run of 2020 – 5.89km

I have not run properly in a good while so I found today's run quite tough actually. It was definitely not an easy run - given that Christmas and New Year's had me eat a lot of food and frankly, I've not been eating with health in mind - let's just say. So as part of...

Happy New Year #2020 from London

Happy New Year #2020 from London

It's now officially 13 minutes past, and we're well into 2020. For the past 3 years, I've been going to Hampstead Heath Park with Victoria Levine, to celebrate with the many hundreds that turn up to watch the London firework celebrations from afar: Here you can see a...