Whenever i call a c code executable from Python using the method below I get a "Bad file descriptor" error. When I run the code from the command prompt it works fine. Help please!?
import subprocess
p = subprocess.call(['C:\Working\Python\celp.exe', '-o', 'ofile'], stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
In [84]: %run "C:\Working\Python\test.py"
Error: : Bad file descriptor
You forgot to add the stdout flag. add stdout = subprocess.PIPE, like this:
p = subprocess.call(
['C:\Working\Python\celp.exe', '-o', 'ofile'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, # needed for the next line to be sensible
stderr=subprocess.STDOUT,
)
now, try run your script again
Try this
import subprocess
p = subprocess.call(['C:\Working\Python\celp.exe', '-o', 'ofile'], stdin=subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
Related
I would like to run a command in python using subprocess.run
I would like to switch the working directory JUST for the execution of this command.
Also, I need to record the output and the return code.
Here is the code I have:
import subprocess
result = subprocess.run("echo \"blah\"", cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
but this only returns
FileNotFoundError: [Errno 2] No such file or directory: 'echo "Running ls -la" && ls -la'
I also tried using the following arguments:
subprocess.run(["echo", "\"blah\""], cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Like Jean-François Fabre said, the solution is to add "shell=True" to the call
import subprocess
result = subprocess.run("echo \"blah\"", cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
shell=True seems to tell subprocess to use the string as a command.
I have a file: "abc", it's a executable file.
I want to execute it by Phthon or windows CMD.
If I write code:
subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")
Then, abc executed, but params(-a -b -c) been ignored
So...How Can I reslove it?
Why not change your code to the following version
args = ['C:\\abc', '-a', '-b', '-c']
p = subprocess.Popen(' '.join(args), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
or without shell=True you can further reduce it to
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
or you can use another method to get output from your exe and pass arguments to it
Output = subprocess.check_output(' '.join(args), shell=True).decode()
Finally, I found the answer myself, which is to use win32process.CreateProcess
Thanks to other authors, but their answers are wrong.
The correct answer is:
win32process.CreateProcess(r"C:\abc", "-a -b -c", None, None, 0, 0, None, None, win32process.STARTUPINFO())
The following answer does not work:
subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")
#or
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Maybe I really don’t know how to use subprocess, but Windows is really special. Executable files without a suffix cannot be executed directly on the command line.
In PowerShell
& "C:\abc.exe"
Hey i'm trying to run a shell Script with python using the Following lines:
import subprocess
shellscript = subprocess.Popen(["displaySoftware.sh"], stdin=subprocess.PIPE)
shellscript.stdin.write("yes\n")
shellscript.stdin.close()
returncode = shellscript.wait()
But when I run the Program it says that it can't find the .sh file.
Your command is missing "sh", you have to pass "shell=True" and "yes\n" has to be encoded.
Your sample code should look like this:
import subprocess
shellscript = subprocess.Popen(["sh displaySoftware.sh"], shell=True, stdin=subprocess.PIPE )
shellscript.stdin.write('yes\n'.encode("utf-8"))
shellscript.stdin.close()
returncode = shellscript.wait()
This method might be better:
import subprocess
shellscript = subprocess.Popen(["displaySoftware.sh"], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
returncode = shellscript.communicate(input='yes\n'.encode())[0]
print(returncode)
When running this on my machine the "displaySoftware.sh" script, that is in the same directory as the python script, is successfully executed.
We're having an issue kicking off a subprocess.Popen within an airflow operator. We're using the following code to kick off sqlcl:
import subprocess
cmd = '/usr/local/bin/sqlcl -V'
p = subprocess.Popen(
cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
executable='/bin/bash')
for line in iter(p.stdout.readline, ''):
self.log.info('%s', line)
p.wait()
# we have also tried p.communicate() and p.poll() here
The snippet above works when run from ipython but hangs with no output when run from within airflow. Any suggestions?
How to execute multiple commands to a command line program(.exe) one by one using python. I need to send commands and need to read the reply on the command line for each command. All in one session since login and settings has to be done.
I tried the below code(python 3.6) but it's not working
from subprocess import Popen, PIPE
process = Popen( "cmd.exe", shell=False, universal_newlines=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE )
cmd= "\"C:\temp\demo.exe\"\n"
out, err = process.communicate(cmd)
print(out)
out, err = process.communicate( 'login\n' )
print(out)
this way you can execute multiple commands
import subprocess
command_list=["C:\\temp\\demo.exe","echo hello"]
for command in command_list:
proc = subprocess.Popen(command, shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = proc.communicate()
print("{} : {}".format(command,out.decode()))
Because shell=True is not the need for cmd.exe