简体   繁体   中英

How to execute a python file in command with different arguments

I have a python file which accept four arguments as input. I run the python file in Anaconda environment by this command.

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

May I know any ideas to execute this command for many times with different userid and filename arguments? Any batch file could help to do this?

Thanks for your help!

One way actually involves python - make a script that activates the original script with different arguments each time, with something like that:

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

Another option using the same "storage" method is, but with a different string formatting way is:

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

There are other options, including user input or importing data from a file(an excel sheet for example), etc, even not using python. This is just a simple example that stays in what you've already delved a bit in:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM