Friday, February 10, 2012

Awesome Google Python Workshop and Windows Issues

The Google Python Workshop is an awesome way to start learning Python, however some of the advanced exercises come a bit unstuck on windows, since certain standard *nix things like wildcards on the command line don't quite work as expected.

For example the way to use wildcards in the babynames exercise at the windows cmd prompt is in fact this:

FOR %G IN ("baby*.html") DO "python" babynames.py --summaryfile "%G"
FOR %G IN ("*.summary") DO "findstr" Trinity "%G"

See http://ss64.com/nt/for2.html for more details on that syntax

There are also a couple of issues in the copyspecial exercise the windows zip program linked to on the site no longer being available (I got it wot work with 7zip)

http://www.7-zip.org/

[note that you'll need to ensure 7z is on the path once you've downloaded it]

and then there is an issue that the commands.getstatusoutput call doesn't work on windows:

http://stackoverflow.com/questions/1193583/what-is-the-multiplatform-alternative-to-subprocess-getstatusoutput-older-comma

subprocess.Popen appears to be required on Python 2.7.2 on Windows, so the final code for this was:
def zip_to(path, zip):
#zip the file in path into "zip" the filename
command = '7z a ' + zip + ' ' + ' '.join(path)
print "Command I'm going to do:" + command
pipe = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
output = pipe.stdout.readlines()
sts = pipe.wait()
print sts
print output

There is also a discussion forum to address issues like these. At least the commands.getstatusoutput issue is mentioned there. However, it would be great if the original documentation could be updated to save a few more unfortunate windows users burning time on this when they want to be learning Python :-)

And for those searching by errors here are the three relevant to the above solutions:

babynames: IOError: [Errno 22] invalid mode ('rU') or filename: 'baby*.html'

copyspecial: '{' is not recognized as an internal or external command, operable program or batch file.

copyspecial: 'zip' is not recognized as an internal or external command, operable program or batch file

No comments: