how to simulate tar filename substitution across piped subprocess.Popen() calls?

Hans Mulder hansmu at xs4all.nl
Wed Nov 14 09:22:52 CET 2012


On 13/11/12 22:36:47, Thomas Rachel wrote:
> Am 12.11.2012 19:30 schrieb Hans Mulder:
> 
>> This will break if there are spaces in the file name, or other
>> characters meaningful to the shell.  If you change if to
>>
>>          xargsproc.append("test -f '%s/{}'&&  md5sum '%s/{}'"
>>                               % (mydir, mydir))
>>
>> , then it will only break if there are single quotes in the file name.
> 
> And if you do mydir_q = mydir.replace("'", "'\\''") and use mydir_q, you
> should be safe...

The problem isn't single quotes in mydir, but single quotes in the
files names that 'tar' generates and 'xargs' consumes.  In the shell
script, these names go directly from tar to xargs via a pipe.  If the
OP wants to do your replace, his script would have to read the output
of tar and do the replace before passing the filenames down a second
pipe to xargs.

However, once he does that, it's simpler to cut out xargs and invoke
"sh" directly.  Or even cut out "sh" and "test" and instead use
os.path.isfile and then call md5sum directly.  And once he does that,
he no longer needs to worry about single quotes.

The OP has said, he's going to d all that.  One step at a time.
That sounds like a sensible plan to me.


Hope this helps,

-- HansM




More information about the Python-list mailing list