I have a python code (cmd.py
) which accepts files as command line arguments and process using parse_args. I want to pass files from another code (files.py
) to cmd.py
, just like passing function arguments.
Can this be done without modifying cmd.py?
|
||||
Yes it can be done:
Then first files.py is executed and the output (Example: "file1.txt file2.txt") is the input for the cmd.py. Example:
This "cats" all the files found by ls in the working directory. |
|||||
|
There are a couple of things that can be done here. First is simple command substitution:
One problem here is that if there are any spaces, tabs or newlines in filename then these filenames won't be passed as a single argument and instead split into different arguments where these characters occur. One alternative is, if
This means that only filenames which contain a newline are a problem, cutting down the number of cases where it will fail. Another problem is what happens if there are a larger number of files output from
Of course this also has potential for problems since it expects filenames that contain problematic characters to be quoted/escaped as you would when entering their names directly in the shell. One option is to change
Finally, perhaps the best way (now supported by most
|
|||||
|
files.py
output a list of files tostdout
that you want to hand tocmd.py
, or do you want to callcmd.py
from withinfiles.py
, like a function? The answers up to now handle the first case. (And in the second case, your question would be better placed on stackoverflow.com.) – Dubu Apr 11 '14 at 10:27