使用 xargs(或其他什么?)顺序执行 python 脚本

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

这个问题几乎与 this question 重复,但只是几乎重复,而且 none 的答案符合我想要实现的目标。

我想执行以下操作:有一个文件名列表,对每个文件名按顺序 (!) 执行 python 脚本。现在,我知道我可以轻松地解析我的 python 脚本中的文件名列表并在每一行上执行任何函数,但这里的想法是我想将批处理执行的灵活性转移到 shell,并保持我的 python 脚本不变。

所以我的参数文件如下所示:

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

python 脚本:

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

我试过了:

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

但我想要实现的是以下内容

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']

我想 xargs 的工作方式让我很困惑。或者它只是错误的工具,总是组合参数并执行单个执行,而不是单独执行?如果是,什么是 xargs 的合适替代品?

感谢帮助

尝试使用 -I:

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

请注意,如果您的任何文件名包含换行符,这将失败,但这是尝试在文本文件中记录文件名所固有的,因此这可能不会成为问题。