I am using Python 3.10.6 and Miniconda 3
What I'm trying to do is write a python script which will open a .cmd file. simple enough, but the problem is it has to be opened in anaconda. I've been able to have my script open the .cmd through the standard command line, and I've been able to have the script open anaconda. But I'm unable to do both in sequence, open the anaconda terminal and then pass a command to it, instructing it to open the .cmd
opening anaconda:
path = 'C:\\Users\...\shortcuts\\conda.lnk'
cmds = 'C:\\Users\\...test.cmd'
cmd_result = subprocess.run(path, shell=True, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True )
print(cmd_result.args)
print(cmd_result.stdout)
this works properly when i open either anaconda path or test.cmd directly using the subprocess.run function. but when I attempt to open test.cmd as a command argument in the anaconda terminal, it doesn't work
path = 'C:\\Users\...\shortcuts\\conda.lnk'
cmds = [path,'/C','C:\\Users\\...test.cmd']
cmd_result = subprocess.run(cmds, shell=True, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True )
I believe this is the relevant part of the error code it returns
File "C:\Users\...dream_restart_module.py", line 17, in dream_restart
cmd_result = subprocess.run(cmds, shell=True, check=True,
File "C:\Users\...Python310\lib\subprocess.py", line 524, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['C:\\Users\\...shortcuts\\conda.lnk', '/C', 'C:\\Users\\...test.cmd']' returned non-zero exit status 1.
I feel like I am missing something obvious here, perhaps about the syntax or limitations of subprocess. Any guidance would be appreciated
When you use subprocess.run() with shell=True, the arguments you give it as part of your cmds list are passed directly to the shell, rather than as arguments to the initial command.
So if you intend to pass '/C' and 'C:\\Users\\...test.cmd' as arguments to path, you should either not declare shell=True (preferred) or provide your cmds as a single, space-separated string (less preferred), like path+' /C C:\\Users\\...test.cmd' or ' '.join(cmds).
Related
I have created a Python script and compiled it into an exe file with PyInstaller. In the process, I have specified the -w option to get an app that doesn't have any console.
Everything works fine except the execution of commands using popen:
mout = subprocess.Popen(['ls','C:\'])
This line generates an exception [Error 6] The handle is invalid.
I have tried adding the parameters
stdout=subprocess.PIPE, stderr=subprocess.PIPE but it stills not working. I think that is because the main process doesn't have any console assigned. I want to execute the command but without opening any shell, it has to be transparent to the user.
Is there any option?
This should solve your issue.
proc = subprocess.Popen(['ls','C:\\'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=CREATE_NO_WINDOW)
On my Mac, I am using the following command to call g++ in python:
subprocess.run(['g++', './rmc-output/*.cpp', '-w', '-o', 'executable'], check=True,
stderr=PIPE, stdout=PIPE, shell=True)
however, I get the following error while the rmc-output folder is not empty.
clang: error: no such file or directory: './rmc-output/*.cpp'
Am I missing something?
shell=True won't expand wildcards when arguments are put in a list. Quickfix: use a string:
subprocess.run('g++ ./rmc-output/*.cpp -w -o executable', check=True,
stderr=PIPE, stdout=PIPE, shell=True)
quick but dirty, so a better solution:
drop shell=True (avoid whenever possible, security issues, lazy command lines...)
use glob to compute files using python, not the shell
like this:
import glob
subprocess.run(['g++'] + glob.glob('./rmc-output/*.cpp') +['-w', '-o', 'executable'], check=True,
stderr=PIPE, stdout=PIPE)
note that Windows versions of g++ have internal wildcard expansion to make up for the fact that Windows "shell" hasn't. Would have worked on Windows probably.
*.cpp is a shell wildcard pattern. Since you're specifying the command arguments directly, wildcard filename expansion never happens, and it's using *.cpp as a literal filename.
See this answer https://stackoverflow.com/a/9997093/494134 for more details.
In python I know how to launch a command using subprocess.Popen
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
what I have to do is: 1) call an executable .exe command, 2) then after that command is running within it call another command.
So when I do 1) like this:
process = subprocess.Popen("C:\Automation\helpFiles\topCmd.exe", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
once that is running I need to run another command i.e state
how can I do it?
I want to run a docker command from python using the subprocess Popen:
proc = subprocess.Popen(
shlex.split(r'docker run -v $PWD:/data blang/latex pdflatex main.tex'),
cwd=temp_dir, shell=False, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
While the command from the terminal works perfect, this returns:
(b'',
b'docker: Error response from daemon: create $PWD: "$PWD" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.\nSee \'docker run --help\'.\n')
"$PWD" is a shell expansion. If you don't have a shell (as with shell=False), it doesn't get expanded.
'%s:/data' % os.getcwd() is a Python expression which will have the same result as "$PWD:/data" in shell. Thus:
import os, subprocess
proc = subprocess.Popen(
['docker', 'run',
'-v', '%s:/data' % os.getcwd(),
'blang/latex', 'pdflatex', 'main.tex'],
cwd=temp_dir, shell=False, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
It's important not to use shlex.split() in this case: If you did, and were in a directory with spaces in its name, each segment of that directory would become a separate argument.
Suppose I run a command cmd="start python Testscript.py" using subprocess in python.
subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Is there any way to get the output of the new command console ?
Is there any way to get the return code from the TestScript.py into the calling python function.