subprocess.Popen('start') fails - python

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)

Related

How can I make python open a cmd and set : cd and then command that I need to use

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.

Python CMD Directory Navigation

Ok, so I'm making a sort of Python middle man. Basically, it takes user input and throws it at Command Prompt, and then returns the output. Here's the code.
import os
console=True
while console==True:
command=input(">")
os.system(command)
The issue is that directory navigation does not seem to be working. The following is what happens when I use the cd command.
>cd
C:\Users\Username\Desktop\Stuff
>cd ..
>cd
C:\Users\Username\Desktop\Stuff
Any ideas?
the problem is that when you execute the cd command it is executed in another context, it is another independent process because your main process never changes directory. you should use the os.chdir (path) method
Changing working directory cannot be done via external commands. You have to parse the command line by yourself:
command = input("> ")
cmd = command.split()
if cmd[0] == "cd":
os.chdir(cmd[1])
Of course, the above code is only an example because the use of str.split() is too wild. You need to take care of quotes and escaped spaces if you want a fully working "shell".

Launch python script with abaqus command

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'])

os.system(<command>) execution through Python :: Limitations?

I'm writing a python (ver 2.7) script to automate the set of commands in this Getting Started example for INOTOOL.
Problem: When I run this entire script, I repeatedly encounter these errors:
Current Directory is not empty
No project is found in this directory
No project is found in this directory
But, when I run a first script only up till the code line marked, and manually type in the next three lines, or when I run these last three lines (starting from the "ino init -t blink" line) after manually accessing the beep folder, then I am able to successfully execute the same code.
Is there a limitation with os.system() that I'm encountering?
My code:
import os,sys
def upload()
os.system("cd /home/pi/Downloads")
os.system("mkdir beep")
os.system("cd beep") #will refer to this code junction in question description
os.system("ino init -t blink")
os.system("ino build")
os.system("ino upload")
sys.exit(0)
Yes, when os.system() commands are run for cd , it does not actually change the current directory for the python process' context. From documentation -
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.
So even though you are changing directory in os.system() call, the next os.system call still occurs in same directory. Which could be causing your issue.
You shoud try using os.chdir() to change the directory instead of os.system() calls.
The Best would be to use subprocess module as #PadraicCunningham explains in his answer.
You can use the subprocess module and os.mkdir to make the directory, you can pass the current working directory cwd to check_callso you actually execute the command in the directory:
from subprocess import check_call
import os
def upload():
d = "/home/pi/Downloads/beep"
os.mkdir(d)
check_call(["ino", "init", "-t", "blink"],cwd=d)
check_call(["ino", "build"],cwd=d)
check_call(["ino", "upload"],cwd=d)
A non-zero exit status will raise CalledProcessError which you may want to catch but once successful you know the commands all returned a 0 exit status.

python in applescript: subprocess.call vs os.system in automator

I found a work around when calling a python script from a shell script from automator (version 2.1.1 in mac os x 10.6.8) (python 2.6.1). My issue is that os.system("mkdir foo") works in a (very specific) case where subprocess.call("mkdir foo") also makes the directory but then makes automator throw an error.
The purpose of my automator app is to accept an image that is dragged-and-dropped onto it.
The automator app has just one action: 'run shell script' with the following code:
for f in "$#"
do
python dphoto_pythons/uploader.py $f > dphoto_pythons/auto_output.txt
done
$f is the name of the dragged image. ('Run shell script' is set to pass input as arguments.)
Here is the strange part:
in the 'uploader.py' script, I was calling this:
retcode=call("mkdir " + dir_full, shell=True) # THIS CAUSES A ERROR IN AUTOMATOR
print "***DOESN'T GET HERE WHEN RUN IN AUTOMATOR****"
It makes the directory. but doesn't get to the next statement. And automator would throw up an error dialog (which just says that I should go check my script).
By contrast, if I called uploader.py 'manually' from the terminal:
"python uploader.py someimage.jpg"
it worked without a hitch.
After puzzling over this for a bit, I tried replacing call with os.system:
os.system("mkdir " + stu_dir_full)
####retcode=call("mkdir " + stu_dir_full, shell=True) # BUG
This works from automator and from terminal.
I have a feeling that I'm over-looking something obvious because this is such a bizarre problem. But thought I'd put this out there in any case. Any thoughts?
-Mel
You could use os.mkdir(stu_dir_full)...
But the problem with your subprocess.call line might be that subprocess.call is expecting a list not a string. Try this:
retcode = call(["mkdir",dir_full], shell=True)

Categories

Resources