10

I have around 20 python files.

Each time I run these files in the terminal this form one after another :

python a.py
python b.py
python c.py
python d.py
python e.py
python f.py
python g.py
.
.
.

(I have provided general file names here)

This process takes lot of time.

Is it possible to run these file together one after another through any script..?

If possible, then how..?

Please provide the code if possible...

I came to know through few sites that, using bash script we can do that..

I don't know how to implement it.

If you can suggest any other method, even that would be helpful.

EDITS:

And I need to generate report for each file using nosetests.

The problem I am facing with nosetests is that, it creates an HTML file with the name results.html.

Each time the report is created, the latest HTML file replaces the old HTML file. Beacuse the names are same.

So what I am doing now is, renaming the report each time i run the nosetests for a python file. Run the nosetests for second file, report gets generated, and rename it.. it goes on...

If somebody could tell me how I can overcome this, It would be helpful..

I am working on Ubuntu 14.04

Karthik
  • 315
  • 1
  • 4
  • 16

4 Answers4

12

To run dynamically all the python scripts in a given folder YOUR_FOLDER, you could run bash script like:

#!/bin/bash

for py_file in $(find $YOUR_FOLDER -name *.py)
do
    python $py_file
done
mvelay
  • 1,520
  • 1
  • 10
  • 23
8

If you want them ran in parallel there's some useful info in this question How do you run multiple programs in parallel from a bash script?

Your command should look like:

python a.py & python b.py & python c.py ....

If you want them to run one after another then replace the & with ;

python a.py;python b.py;python c.py ...

Hope it helps!

Community
  • 1
  • 1
Lucian2k
  • 307
  • 2
  • 5
1

You could use a simple bash script which will execute your commands one after another :

#!/bin/bash
python a.py
python b.py
python c.py
python d.py
python e.py
python f.py
python g.py
.
.
.

If your script is meant to be used on multiple platforms I highly recommand you to precise the python version to execute (python2 or python3).

EDIT : If you need to execute all of the python scripts in your folder, you'd better use a for loop like massiou suggested in his answer.

Aurel
  • 631
  • 3
  • 18
  • What if I need to generate the report of the test cases using nosetest ? Can I do it the same way ?! `nosetest a --with-html-out ` `nosetest b --with-html-out ` `nosetest c --with-html-out ` etc etc.. in bash script. – Karthik Apr 25 '16 at 09:36
  • 1
    Just replace `python a.py` by `nosetest a.py --with-html-out`, etc. You can add any command line you'd use in your console. – Aurel Apr 25 '16 at 09:46
  • It generates results.html file for all the tests. The name is common. So finally I get just 1 html file. What I need is separate names for each html files. How can I do that ? – Karthik Apr 26 '16 at 05:03
0

You can use bash wildcard

create sample file

for f in  test_python{1..9}.py; do echo  "print __file__" > $f; done

File are indexed

ls test*

test_python1.py test_python2.py test_python3.py test_python4.py test_python5.py test_python6.py test_python7.py test_python8.py test_python9.py

Run them in desired order:

for f in  test_python{1..9}.py; do python $f ; done
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56
  • My test files are not exactly the same like in the question. Names are totally different. Eg: `audio.py`, `playback.py`, `controls.py` etc etc – Karthik Apr 25 '16 at 09:59
  • you can make a symlink ln -s playback.py test_python1.py then run test_python – Ali SAID OMAR Apr 25 '16 at 10:05