0

experts, i have a 50 nos of python scripts that i want to run one by one on the same folder.But problem is that in the system where i work many people also use it very often.so there is a possibility of misuse of my python scripts.so i want to copy the 50 nos of python scripts to the same folder and delete the python scripts(script1.py,script2.py...) from the same folder after running the master script which content all the running scripts.so i did something like as below

#!/bin/sh
python script1.py
python script2.py
python script3.py
python script4.py
python script5.py
python script6.py
python script7.py
python script8.py
python script9.py
python script10.py
....

but in the above script problem is that if i delete the python scripts only first python script i.e. python script1.py is running not others.I hope experts will help me overcoming this problem.

2 Answers 2

1

You are doing it wrong.

You should write a main.py program which will execute your functions one after the other.

On the way that you are currently using only first one is executed as cli is passed to the script1.py and other are ignored if your script do not exit.

So for example if you made a infinite loop error then yeah other will never run.

I would suggest learning python imports and execution.

One way is:

# main.py you have the script1.py and others in same folder.
# you need to have the scripts inside of the functions so you could as well do that in same file.
# so script1 should look like:
def script1_func(): # any arguments that are needed can be passed
    # code of your script here
    return value # if your script returns some value write it there

# then in main.py
import script1
import script2
import script3

if __name__ == '__main__':
    script1.script1_func()
    script2.script2_func()
    script3.script3_func()

# on that way you can execute them all in sequence. If needed to use paralelism check multithreading.
# if you need to remove them or any other files.
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# this will give you a path to the file and then you just issue delete
if os.path.exists(basedir + "script1.py"):
    os.remove(basedir + "script1.py")
else:
  print("The file does not exist")
os.rmdir(basedir) # this will delete whole folder

Btw it is better to use better conventions and do not duplicate similar functions.

https://gist.github.com/MarkoShiva/7584151b08a095a1708bdee339f61a65

Other way to do the same is to run a exec on each of the files from bash which might lead to too much load of the machine if you have more scripts then cores.

That is other answer. So other user suggested you using find command to execute scripts in parallel.

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

2 Comments

can you please provide me an example script should i include all python scripts inside the main.py
where is the link..can u suggest
0

For your exact needs, you can use below solution

I have create 2 sample python files and a shell script to invoke both the scripts.

➜  65976750 ls
sandex.sh  script1.py script2.py

Debug mode execution

➜  65976750 bash -x sandex.sh
+ mkdir ./sandexec
+ find . -name '*.py' -exec cp '{}' ./sandexec ';'
+ python3 ./sandexec/script1.py
running python script 1
+ python3 ./sandexec/script2.py
Running from script2
+ rm -rf ./sandexec

original script

➜  65976750 cat sandex.sh
mkdir ./sandexec
find . -name "*.py" -exec cp {} ./sandexec \;
python3 ./sandexec/script1.py
python3 ./sandexec/script2.py
rm -rf "./sandexec"

➜  65976750 ls
sandex.sh  script1.py script2.py
➜  65976750

3 Comments

please make it easier its little confusing ➜ 65976750 what is this
will it work if i delete the python scripts from same folder
it doesnot work it deletes the same directory then where do i save output

Your Answer

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