运行目录中的所有Python文件

时间:2011-02-16 10:34:13

标签: python bash matplotlib

在目录中运行所有Python文件的最佳方法是什么?

python *.py

只执行一个文件。在shell脚本(或make文件)中为每个文件写一行似乎很麻烦。我需要这个b / c我有一系列小的matplotlib脚本,每个脚本创建一个png文件,并希望一次创建所有图像。

PS:我正在使用bash shell。

2 个答案:

答案 0 :(得分:27)

bash有循环:

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

答案 1 :(得分:13)

另一种方法是使用xargs。这使您可以并行执行,这在当今的多核处理器上非常有用。

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

-n 1 使xargs只为每个进程提供一个参数,而 -P 3 将使xargs最多并行运行三个进程。