[Tutor] Problem recognizing '{' character?

Peter Otten __peter__ at web.de
Tue Mar 29 10:41:36 CEST 2011


Ben Hunter wrote:

> Hi,
> 
> I'm completing the Python lessons on YouTube that Google posted. At the
> end of section 2 of day 2, there is a task to identify files then put them
> in a zip file in any directory. The code is from the 'solution' folder, so
> it's not something I wrote. I suspect I have a problem with PATHS or
> environment variables. I'm new to programming in something as advanced as
> Python, but I do okay with VBA - so I just feel like there's a setting up
> issue somewhere. I'm on Windows 7, tried running this in Idle and from the
> command line.

The commands module used by zip_to() is an unfortunate choice for a tutorial 
because it is supposed to work with unix shells only.

> def zip_to(paths, zipfile):
>   """Zip up all of the given files into a new zip file with the given
> name."""
>   cmd = 'zip -j ' + zipfile + ' ' + ' '.join(paths)
>   print "Command I'm going to do:" + cmd
>   (status, output) = commands.getstatusoutput(cmd)
>   # If command had a problem (status is non-zero),
>   # print its output to stderr and exit.
>   if status:
>     sys.stderr.write(output)
>     sys.exit(1)

You can either try to install Cygwin to run your script unchanged or rewrite 
the above function to work with subprocess

import sys
from subprocess import Popen, PIPE

def zip_to(paths, zipfile):
    command = ["zip", "-j", zipfile]
    command.extend(paths)
    process = Popen(command, stdout=PIPE, stderr=PIPE)
    stdoutdata, stderrdata = process.communicate()
    if process.returncode:
        sys.stdout.write(stdoutdata)
        sys.stderr.write(stderrdata)
        sys.exit(1)

You'll still need a zip.exe on your system.
If you're ambitious, have a look at

http://docs.python.org/library/zipfile.html

a library that allows you to create zipfiles in python without the help of 
an external program.



More information about the Tutor mailing list