bash 运行目录中的所有 Python 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5015316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run all Python files in a directory
提问by Gerald Senarclens de Grancy
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. 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.
只执行一个文件。在 shell 脚本(或 make 文件)中为每个文件编写一行似乎很麻烦。我需要这个 b/c 我有一系列小的 matplotlib 脚本,每个脚本创建一个 png 文件,并希望一次创建所有图像。
PS: I'm using the bash shell.
PS:我正在使用 bash shell。
回答by Cat Plus Plus
bash has loops:
bash 有循环:
for f in *.py; do python "$f"; done
回答by Erik Forsberg
An alternative is to use xargs. That allows you to parallelise execution, which is useful on today's multi-core processors.
另一种方法是使用 xargs。这允许您并行执行,这在当今的多核处理器上很有用。
ls *.py|xargs -n 1 -P 3 python
The -n 1makes xargs give each process only one of the arguments, while the -P 3will make xargs run up to three processes in parallel.
所述-n 1个品牌xargs的给每个进程仅其中一个参数,而-P 3将使xargs的运行多达三个并联处理。

