Computer >> Computer tutorials >  >> Programming >> Python

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


The fastest and easiest way to run all Python files in a directory is to use loops. You can use bash to do this for you.

Example

For example, create a new file called run_all_py.sh and write the following in it:

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

Output

 Now run the file using 

$ bash run_all_py.sh

 You could also use xargs to parallely execute these files(Only available on UNIX).

For example

$ ls *.py|xargs -n 1 -P 4 python