subprocess.popen function with quotes

skunkwerk skunkwerk at gmail.com
Thu Mar 27 01:33:50 EDT 2008


On Mar 26, 8:05 am, Jeffrey Froman <jeff... at fro.man> wrote:
> skunkwerk wrote:
> > 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!



More information about the Python-list mailing list