Search code examples
pythonxargs

using xargs (or sth else?) to sequentially execute a python script


This question is almost a duplicate of this question, but only almost, and none of the answers fit to what I a trying to achieve.

I want to do the following: have a list of filenames, execute a python script sequentially (!) with each filename. Now, I know I can easily parse the list of filenames in my python script and execute whatever function on each line, but the idea here is that I want to move that batch execution flexibility to the shell, and leave my python scripts unchanged.

So my file of arguments looks like this:

$ cat arglist
1                  # <- separated by newlines
2
3
4

the python script:

import sys
print("args received:",sys.argv)

I tried:

$ cat arglist | xargs python ./pyxargs.py -a -b -c
args received: ['./pyxargs.py', '-a', '-b', '-c', '1', '2', '3', '4']

But what I am trying to achieve is the following

args received: ['./pyxargs.py', '-a', '-b', '-c', '1']
args received: ['./pyxargs.py', '-a', '-b', '-c', '2']
args received: ['./pyxargs.py', '-a', '-b', '-c', '3']
args received: ['./pyxargs.py', '-a', '-b', '-c', '4']

I guess the way how xargs works is just confusing me. Or is it simply the wrong tool, always combining the arguments and executing a single execution, instead of separate executions? If yes, what would be the appropriate alternative to xargs?

Thanks for help


Solution

  • Try using -I:

    < arglist xargs -I file python ./pyxargs.py -a -b -c file

    Note that this will fail if any of your filenames contains a newline, but that's inherent in trying to record filenames in a text file so this probably won't be an issue.