I have a command file (.cmd) which I use to launch Abaqus command line windows.
Then, I use the command 'abaqus python test.py' to launch python command inside Abaqus.
Now, I would like to use a python script to do that.
I try something like this but doesn't work. Someone know the trick ?
Thanks !!
import subprocess
AbaqusPath=r"C:\Abaqus\script\abaqus.cmd"
args= AbaqusPath + "-abaqus python test.py"
subprocess.call(args)
Using .cmd-file:
This way might work with cmd file:
abaqusPath = "C:\\Abaqus\\script\\abaqus.cmd /C"
args = AbaqusPath + "abaqus python test.py"
subprocess.call(args)
Flag /C is needed to run command and then terminate.
Easiest way:
Just add the folder with abaqus commands (typical path C:\Abaqus\Commands) into the PATH variable of the system. This will give the access to commands like abaqus, abq6141 etc. in cmd directly.
When just use the following in your script:
subprocess.call("abaqus python test.py")
Using .bat-file:
If the configuration of PATH variable is impossible and the first way does not work, .bat-files from abaqus can be used as follows:
abaqusPath = "C:\\Abaqus\\Commands\\abaqus.bat "
args = AbaqusPath + "python test.py"
subprocess.call(args)
I've never had any success using just string arguments for subprocess functions.
I would try it this way:
import subprocess
abaqus_path = r"C:\Abaqus\script\abaqus.cmd"
subprocess.call([abaqus_path, '-abaqus', 'python', 'test.py'])
Related
I want to execute another program that I compiled earlier from python using the subprocess.call(command) function.
However, python states that it cannot find the command. I suspect that subprocess is not able to find my custom command since it does not know the PATH variable of my Ubuntu system. Is it possible to somehow execute the following code, where command is part of my PATH?
import subprocess
subprocess.run("command -args")
Running this code leads to the error command not found.
You can either provide the explicit path to your command:
subprocess.run('/full/path/to/command.sh')
or else modify your PATH variable in your Python code:
import os
os.environ['PATH'] += ':'+'/full/path/to/'
subprocess.run('command.sh')
You can modify the environment variables. But be careful when you pass arguments.
Try something like this:
import os
import subprocess
my_env = os.environ.copy()
my_env["PATH"] = "/usr/test/path:" + my_env["PATH"]
subprocess.run(["command", "-args"], env=my_env)
I want to make script that will open and pre set cmd with scrcpy commands.
I tried modules like os,subprocess,pyautogui
When I tried to open cmd with os and type inside it with pyautogui it didnt work
What should I use to type commands.
All I need to write is 'cd "directory"' and scrcpy but I cant find a way to do it with python
You can use following code:
import os
os.system('cmd /c "Your Command Prompt Command"')
This is the code to do it
import os as os
os.startfile("cmd.exe")
And to run the command use
os.system("Your Command")
This will show the command you passed into the cmd terminal.
I have a function in my python script that needs to load a python shell.
run('python /my/script', shell=True)
The problem is that when I type something, the bash shell is providing output instead of the python shell.
Do you know why and how can I fix it?
You should use the subrocess Python module. If path of your Python executable is set as environment variable, you need to write only python when you call the another Python file, in other case you need the write the full path of your Python executable.
test.py Python file:
import subprocess
process = subprocess.Popen(["python", "other.py"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
std_out, std_err = process.communicate()
print(std_out)
other.py Python file:
print("Hello from other Python file.")
output of test.py Python file:
>>>python test.py
Hello from other Python file.
Running this in python will result in a WindowsError stating it cannot find the specified file
FAILS:
import subprocess
subprocess.Popen('start notepad.exe')
In a command window, it works
start notepad.exe
Im guessing its a path thing where windows can't locate start[.exe?] Where is this located so i can add it in the path or just include it in the Popen call.
Thanks
I'm not entirely sure start is a program. I think it might be a built-in command of the CMD shell. Try
subprocess.Popen('cmd /c start notepad.exe')
Also, any reason why not use just:
subprocess.Popen('notepad.exe')
notepad = subprocess.Popen(r'start "" "C:\1.txt"', shell=True)
time.sleep(3)
print(notepad.pid)
I have a script in my project's bin directory, and I want to execute it from a cron. Both scripts are written in python.
Target file :
App_directory/bin/script_name
Want to execute script_name script with some parameters from App_directory/cron/script_name1.py
How do I achieve that ?
Try:
import os
os.system('/path/to/App_directory/bin/script_name')
Or if script_name is not executable and/or doesn't have the shabang (#!/usr/bin/env python):
import os
os.system('python /path/to/App_directory/bin/script_name')
The subprocess module is much better than using os.system. Just do:
import subprocess
subprocess.call(['/path/to/App_directory/bin/script_name'])
The subprocess.call function returns the returncode (exit status) of the script.
It works for me...
import subprocess
process = subprocess.Popen('script_name')
print process.communicate()