python run multiple scripts - python

Hi i would like to run add1.py and add2.py simultaneously and have searched BAT file and SH file but couldnt do it myself. Anyone can help me? The folder is in the following path C:\Users\Jia\Downloads\Telegram Bot\Scripts
I might also add more scripts like add3.py add4.py and the list goes on. Does anyone have simple tips that can help me run every script in this folder? Thank you!
It would be even better if the script runs one after another, example add2.py runs after add1.py finishes.

Just run: python add1.py & python add2.py. If you only want the second one to run if the first executes successfully, use python add1.py && python add2.py.
Running them at the same time would use something called concurrency, which would require some modifications to your script.
NOTE: This will only work on Windows. On Linux or MacOS, you would use: python add1.py ; python add2.py
You can manually add more scripts. To runn every python file in a folder, you could use: python *.py if you imported them all as modules into a new file called main.py and executed them in what ever order you like in that file.

As someone else already suggested, you could make a python file which executes your N python scripts.
Using subprocess as described here: https://stackoverflow.com/a/11230471/11962413
import subprocess
subprocess.call("./test1.py", shell=True)
subprocess.call("./test2.py", shell=True)

You can try this, if you are just tying to run python file:
import os
lst=[l for l in os.listdir() if l.endswith(".py")]
for ls in lst:
os.system(f'python {ls}')
Or if the name has some pattern or it is sure then, try this:
import os
for i in range(1,<up to last name+1>):
os.system(f"python add{i}.py")

Related

writing commands in order ( Linux )

i'm new to Linux.
I am learning Linux commands.
I am looking for a way to write command lines after each other from text file for instance.
Instead of typing five or more lines by self i can just call the text file to write commands after each other.
Is this even possible?
Is this a known thing?
If there is anything like please guide me to it.
Thanks in advance.
You can create it by using the subprocess module.
Example :
import subprocess
subprocess.call(["ls"])
subprocess.call(["touch","helloworld.txt"])
# You can enter commands by using subprocess.call()
The you use chmod +x filename.py to make the file executable and the ./filename.py to run the script
Or as suggested by #bipll in the comments, creating an .sh file. Here is the tutorial if you need.

How to run the Linux commands which is saved in a text file from python script?

Suppose I have a file 'commands.txt' which has list of linux commands line-by-line (for example: who, pwd, ls, ps, clear etc.,)
I need a python script wherein when run should execute all the linux commands one-by-one in the console/shell.
I am looking for different approaches to do it.
May be one approach I can think off is using os module in Python. (Please correct me if I missed out anything here)
import os
with open("commands.txt") as file:
for line in file:
os.system(line)
Kindly help me with different approaches to this problem.
-TIA
Like #papey said,
Create a bash script (.sh) file, with the correct shabang (#!/usr/bin/env bash).
You could write in your Python script the following then:
import os
os.system("chmod +x myscript.sh")
os.system("./myscript.sh")

Python script to run command line which starts python script with specific python version

I need some help. Is there a possibility to let python start the command line in windows and let the command line execute a script in another python version on my pc?
Expample: I have two versions of python on my pc. One is within Anaconda and the other one is pure Python. Now I have some scripts I want to be executed in specific order. My problem is, that the Google Analytics API doesn't work with Anaconda and some other packages (like Simpy) doesn't work with pure Python. So I need to work with two different versions of python for one project.
Now I want to write a litte python file, which opens the command line and executes the scrips in specific order on my different Python-versions.
I know how to run a python file on the command line. It's via
C:\path_to_python\python.exe C:\path_to_file\file.py
But how can I make a python script executing that line above in the command line?
Hope someone can help me.
Thanks.
import os
os.system("C:\path_to_python\python.exe C:\path_to_file\file.py")
os.system() returns the command's exit value so if you need some output from the script this won't work.
I suggest you look at subprocess
# this is new to python 3.5
import subprocess
cmd = subprocess.run(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"], stdout=subprocess.PIPE)
return_string = cmd.stdout
# alternative for getting command output in python 3.1 and higher
import subprocess
return_string = subprocess.check_output(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"])
Instead you can try writing a batch file in which you can specify the order how you want to run the files and with which version you have to run the file.
lets say first i want to run a file in python2.7 and the later in python3.4 and my files were in d:/pythonfiles
RunningSequence.bat
d:
cd D:\pythonfiles
c:\python27\python.exe python27file.py
c:\python34\python.exe python34file.py
try this and let me know :
import sys
with open(sys.argv[1], 'r') as my_file:
exec(my_file.read())

importing the wx module in python

When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.

How to run a Python script from another Python script in the cross-platform way?

Here is the problem...
I'm writing very small plugin for Blender,
I have 10 python scripts, they parsing different file formats by using command-line, and I have a Main Python script to run all other scripts with proper commands...
for example, "Main.py" include:
txt2cfg.py -inFile -outFile...
ma2lxo.py -inFile -outFile...
Blender already include Python, so I can run "Main.py" from Blender, But I need it to work with both PC and MAC, and also doesn't require Python installation, so I can't use:
execfile(' txt2cfg.py -inFile -outFile ')
os.system(' ma2lxo.py -inFile -outFile ')
or even import subprocess
because they required Python installation in order to run *.py files.
Sorry for language
Thanks
If you really need to execute a python script in a new process and you don't know where the interpreter you want is located then use the sys module to help out.
import sys
import subprocess
subprocess.Popen((sys.executable, "script.py"))
Though importing the module (dynamically if need be) and then running its main method in another script is probably a better idea.
for example, "Main.py" include:
txt2cfg.py -inFile -outFile...
ma2lxo.py -inFile -outFile...
Two things.
Each other script needs a main() function and a "main-import switch". See http://docs.python.org/tutorial/modules.html#executing-modules-as-scripts for hints on how this must look.
Import and execute the other scripts.
import txt2cfg
import ma2lxo
txt2cfg.main( inFile, outFile )
ma2lxo.main( inFile, outFile )
This is the simplest way to do things.
Two options:
Use py2exe to bundle the interpreter with the scripts.
Import the modules and call the functions automatically.

Categories

Resources