Reputation: 7965
I want to log all calls to subprocess.Popen, so that I can create a batch/shell file that is equivalent.
How does python turn the list of arguments into a command?
Is it equivalent to ' '.join([args])
?
For example:
args=['ls', '-A']
subprocess.Popen(args)
# ...
I want to get an equivalent file to all the calls:
ls -A
Upvotes: 1
Views: 193
Reputation: 9161
No, it's not equivalent to that (since some of the arguments might have spaces or other shell meta-characters in them). In fact, unless you use shell=True, Python doesn't need to combine all the arguments into one string at all. It uses an exec*()-like call (see man execve) and passes the arguments in an array.
If you just want to output enough information to recreate the argument list later, then you might just want to output repr(args)
.
Since your goal is to make an equivalent batch/shell file, you probably need to take special steps to quote arguments so that any special metacharacters inside them won't affect how the shell splits up the arguments. One way to do that is to pass each argument through a function like the one I put up at https://gist.github.com/790722 (although it doesn't know how to do safe quoting for batch files).
Upvotes: 1