开发者

Run all Python files in a directory

开发者 https://www.devze.com 2023-02-11 23:08 出处:网络
What is the best way to run all Python files in a directory? python *.py only executes on开发者_开发技巧e file. Writing one line per file in a shell script (or make file) seems cumbersome. I need t

What is the best way to run all Python files in a directory?

python *.py

only executes on开发者_开发技巧e file. Writing one line per file in a shell script (or make file) seems cumbersome. I need this b/c I have a series of small matplotlib scripts each creating a png file and want to create all of the images at once.

PS: I'm using the bash shell.


bash has loops:

for f in *.py; do python "$f"; done


An alternative is to use xargs. That allows you to parallelise execution, which is useful on today's multi-core processors.

ls *.py|xargs -n 1 -P 3 python

The -n 1 makes xargs give each process only one of the arguments, while the -P 3 will make xargs run up to three processes in parallel.

0

精彩评论

暂无评论...
验证码 换一张
取 消