如何使用不同的参数在命令中执行python文件

时间:2021-03-13 17:02:50

标签: python batch-file command

我有一个 python 文件,它接受四个参数作为输入。我通过这个命令在Anaconda环境中运行python文件。

python "\python.py" --arg1 "account" --arg2 "userid" --arg3 "action" --arg4 "filename"

我可以知道使用不同的用户 ID 和文件名参数多次执行此命令的任何想法吗?任何批处理文件都可以帮助做到这一点?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

实际上涉及 python 的一种方法 - 制作一个脚本,每次使用不同的参数激活原始脚本,如下所示:

import os  # for os.system, which executes the command in a subshell.

list_of_args = [[1,2,3,4],[a,b,c,d],...,]

for item in list_of_args:
    os.system(f'python "\python.py" --arg1 {item[0]} --arg2 {item[1]} --arg3 {item[2]} --arg4 {item[3]}')

另一个使用相同“存储”方法的选项是:

for item in list_of_args:
    os.system('python "\python.py" --arg1 {} --arg2 {} --arg3 {} --arg4 {}'.format(*item))

还有其他选项,包括用户输入或从文件(例如 Excel 表)导入数据等,即使不使用 python。这只是一个简单的例子,保留在您已经深入研究的内容中 :)

相关问题