Running all Python files in a directory

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

python *.py 

executes only one file. Writing one line to a file in a shell script (or make file) seems cumbersome. I need this b / c. I have a series of small matplotlib scripts, each of which creates a png file and wants to create all the images at once.

PS: I use the bash shell.

+7
source share
2 answers

bash has loops:

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

An alternative is to use xargs. This allows you to execute execution in parallel, which is useful on modern multi-core processors.

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

-n 1 does xargs gives each process only one of the arguments, while -P 3 causes xargs to run up to three processes in parallel.

+10
source

All Articles