Re: [Tutor] Problem recognizing '{' character?

2011-03-29 Thread Ben Hunter
Thanks a ton. For the record I did read the 'command' module help page, but
must have skipped over the 'Platforms: Unix' and 'Deprecated' parts. I
successfully got it to work with the subprocess module, but I really
appreciate you filling out the rest with the sys.stderr.write(stderrdata). I
certainly would have stumbled over that.

-BJH


On Mon, Mar 28, 2011 at 8:12 PM, 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.
>
> These two work perfectly.
>
> def get_special_paths(dirname):
>   result = []
>   paths = os.listdir(dirname)  # list of paths in that dir
>   for fname in paths:
> match = re.search(r'__(\w+)__', fname)
> if match:
>   result.append(os.path.abspath(os.path.join(dirname, fname)))
>   return result
>
>
> def copy_to(paths, to_dir):
>   if not os.path.exists(to_dir):
> os.mkdir(to_dir)
>   for path in paths:
> fname = os.path.basename(path)
> shutil.copy(path, os.path.join(to_dir, fname))
>
> This third one does not.
>
> 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)
>
> My command is this: >>> copyspecial.zip_to(paths, 'zippy')
>
> But something goes wrong and it spits this out:
> '{' is not recognized as an internal or external command,
> operable program or batch file.
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem recognizing '{' character?

2011-03-29 Thread Tim Golden

On 29/03/2011 09:41, Peter Otten wrote:

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.


It's also unfortunate that an issue has been outstanding here for
a while:

  http://bugs.python.org/issue10197

It's one of those which seems simple to fix but which spawns a
thousand discussions...

TJG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem recognizing '{' character?

2011-03-29 Thread Peter Otten
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.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem recognizing '{' character?

2011-03-28 Thread Ben Hunter
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.

These two work perfectly.

def get_special_paths(dirname):
  result = []
  paths = os.listdir(dirname)  # list of paths in that dir
  for fname in paths:
match = re.search(r'__(\w+)__', fname)
if match:
  result.append(os.path.abspath(os.path.join(dirname, fname)))
  return result


def copy_to(paths, to_dir):
  if not os.path.exists(to_dir):
os.mkdir(to_dir)
  for path in paths:
fname = os.path.basename(path)
shutil.copy(path, os.path.join(to_dir, fname))

This third one does not.

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)

My command is this: >>> copyspecial.zip_to(paths, 'zippy')

But something goes wrong and it spits this out:
'{' is not recognized as an internal or external command,
operable program or batch file.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor