简体   繁体   English

运行目录中的所有Python文件

[英]Run all Python files in a directory

What is the best way to run all Python files in a directory? 在目录中运行所有Python文件的最佳方法是什么?

python *.py

only executes one file. 只执行一个文件。 Writing one line per file in a shell script (or make file) seems cumbersome. 在shell脚本(或make文件)中为每个文件写一行似乎很麻烦。 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. 我需要这个b / c我有一系列小的matplotlib脚本,每个脚本创建一个png文件,并希望一次创建所有图像。

PS: I'm using the bash shell. PS:我正在使用bash shell。

bash有循环:

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

An alternative is to use xargs. 另一种方法是使用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. -n 1使得xargs只为每个进程提供一个参数,而-P 3将使xargs并行运行最多三个进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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