1

Is there any way to run multiple .py files that are located in the same directory in terminal?

Example:

Let's say I have three .py files

x.py
y.py
z.py

I have saved these files in the directory abc

In terminal, I would execute x.py as so

python ~/abc/x.py

And would have to repeat that to run y.py and z.py

Is there any way to run all three scripts at the same time ? Or some shortcut? Save time than running multiple terminal windows open ?

Thanks

0

4 Answers 4

1

Assuming bash:

for i in x y z; do python ~/abc/$i.py & done

This loop iterates over the strings "x", "y" and "z". each iteration the variable $i is another one of them. So in the first iteration, $i is x, so ~/abc/$i.py is ~/abc/x.py. The second iteration is the same, only with y, and in the third $i is x.

So bash actually do

python ~/abc/x.py &
python ~/abc/y.py &
python ~/abc/z.py &

Which is what you asked for.

The & makes it run in the background. You can do this for any command you invoke.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I'm not a programmer at all... Can you please let me know what the $i is? And how Im supposed to connect that with file names?
1

I have often used something like (in bash)

for file in $( ls *.py )
  do
    python $file
  done

I use thos when I have many python-files and want to run all of them.

4 Comments

I have tried the command in terminal like this: for file in *.py do python $i done but nothing happened...just the python help menu...
You need to do for file in ~/abc/*.py ; do python $i ; done
For me this worked great after I changed file to i, so for i in $(ls *.py)
Yes, you're right. I have changed it in the answer now. Thank you.
0

Since others have given you shell solutions, let me propose a pythonic one:

from multiprocessing import Process

import x
import y
import z

# Assuming you have a main function f in each module

p1 = Process(target=x.f, args = (1,)) # or whatever args
p1.start()
p2 = Process(target=y.f, args = (2,))
p2.start()
p3 = Process(target=z.f, args = (3,))
p3.start()

This is Python 3.3 code, you might have to do it a little differently in Python 2.7.

For example, if x.f, y.f and z.f are all defined

def f(x):
    print(x)

then the code above will output:

1

2

3

probably, but not necessarily, in that order.

Comments

0

bash file

ls *.py | xargs -I{} sh -c "echo {}; python {}"

you can get all .py file in current directory with command ls *.py

and use xargs -I to execute from standard input

run all .py in directory in sh -c with command python {} automatically replace {} with .py files

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.