subprocess.popen function with quotes

Jeffrey Froman jeffrey at fro.man
Wed Mar 26 11:05:31 EDT 2008


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



More information about the Python-list mailing list