简体   繁体   中英

How to execute a series of Python scripts in batch?

I frequently find that I need to run a series of python scripts sequentially. The order and number of these scripts may vary, but instead of executing each script individually and sequentially, is there a preferred way of batch automating a series of Python scripts? My programs require multiple and complex file path and other argument combinations.

Refactor them into modules, then make a new script that import s the relevant functions from each module and runs those.

The easiest way to do this is to write a main function per module and run all the main function serially.

I think larsmans' option is the best by far. But if there is a reasons you cannot or should not turn them into easily imported modules, it is quite easy to just make a .bat file in Windows (or .ps1 if you have powershell) which calls each of them in turn. In Linux, the same thing can be done with bash or csh as long as you remember to make the resulting batch executable.

If you want another python script that calls them as though they were being called from a command line (in order to pass in the argument combinations without refactoring them into modules) you can readily use os.system or the more powerful (but sligtly more complicated) subprocess module .

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