subprocess.popen function with quotes

skunkwerk skunkwerk at gmail.com
Sun Apr 13 16:54:18 EDT 2008


On Mar 26, 10:33 pm, skunkwerk <skunkw... at gmail.com> wrote:
> On Mar 26, 8:05 am, Jeffrey Froman <jeff... at fro.man> wrote:
>
>
>
> >skunkwerkwrote:
> > > p = subprocess.Popen(['rename','-vn','s/(.*)\.htm$/
> > > model.html/','*.htm'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
> > > print p.communicate()[0]
>
> > > i change to print p.communicate()[1] in case the output is blank the
> > > first time
>
> > > this is the output:
> > > *.htm renamed as model.html
>
> > Without shell=True, your glob characters will not be expanded. Hence, the
> > command looks for a file actually named "*.htm"
>
> > > when I add shell=True to the subprocess command, I get the following
> > > output:
> > > Usage: rename [-v] [-n] [-f] perlexpr [filenames]
>
> > Here the use of the shell may be confounding the arguments passed. Your
> > command will probably work better if you avoid using shell=True. However,
> > you will need to perform your own globbing:
>
> > # Untested (no perl-rename here):
>
> > command = ['rename','-vn', 's/(.*)\.htm$/model.html/']
> > files = glob.glob('*.htm')
> > command.extend(files)
> > p = subprocess.Popen(
> >     command,
> >     stdout=subprocess.PIPE,
> >     stderr=subprocess.PIPE,
> >     )
>
> > Jeffrey
>
> thanks Jeffrey, that worked like a charm!

I'm trying to detect when the subprocess has terminated using the
wait() function - but when there is an error with the call to rename
(ie the file doesn't exist) rename (when run from the command line
just terminates and displays the error).  In the code above, though,
my call to p.wait() just hangs when rename should throw an error...
I've tried adding shell=True but that stops the rename from working.
any ideas?

thanks



More information about the Python-list mailing list