Passing absolute paths to subprocess.call() [Python] - python

I'm working on a script that should call an external executable file, which takes a path as an argument(and generates another file). I used the following syntax:
call(['C:/program.exe', 'C:/input.txt'])
It should generate an output.txt in the same directory.
My problem is that I don't get any output, or error message. If I run that command manually in cmd, everything works fine.
How can I solve this?

You could use Popen to get an error message.
from subprocess import PIPE, Popen
Popen(['C:/program.exe', 'C:/input.txt'], stdout=PIPE)
for line in process.stdout:
print(line)
I also suspect that your executable isn't working, because the working directory is different.

Related

Running script file through subprocess

I'm attempting to run a Linux script through Python's subprocess module. Below is the subprocess command:
result = subprocess.run(['/dir/scripts/0_texts.sh'], shell=True)
print(result)
Here is the 0_texts.sh script file:
cd /dir/TXTs
pylanguagetool text_0.txt > comments_0.txt
The subprocess command executes the script file, writing a new comments_0.txt in the correct directory. However, there's an error in the execution. The comments_0.txt contains an error of "input file is required", and the subprocess result returns returncode=2. When I run the pylanguagetool text_0.txt > comments_0.txt directly in the terminal the command executes properly, with the comments_0.txt written with the proper input file of text_0.txt.
Any suggestions on what I'm missing?
There is some ambiguity here in that it's not obvious which shell is run each time 0_texts.sh is invoked, and whether it has the values you expect of environment variables like PATH, which could result in a different copy of pylanguagetool running from when you call it at the command line.
First I'd suggest removing the shell=True option in subprocess.run, which is only involving another, potentially different shell here. Next I would change subprocess.run(['/dir/scripts/0_texts.sh']) to subprocess.run(['bash', '/dir/scripts/0_texts.sh']) (or whichever shell you wanted to run, probably bash or dash) to remove that source of ambiguity. Finally, you can try using type pylanguagetool in the script, invoking pylanguagetool with its full path, or calling bash /dir/scripts/0_texts.sh from your terminal to debug the situation further.
A bigger-picture issue is, pyLanguageTool is a Python library, so you're almost certainly going to be better off calling its functions from your original Python script directly instead of using a shell script as an intermediary.

Passing text into Windows cmd line using a python script

I am trying to write a simple python script that will open the Windows cmd line, change to a specified directory, and then input the text: 'Test.exe -blah -blahblah etc...' in order to run my Test.exe executable with my specified parameters from the cmd line.
The code I have so far is the following:
import subprocess
subprocess.Popen(r'C:\Windows\System32\cmd.exe', cwd=r'C:\PythonTestScripts')
This code successfully launches the windows cmd.exe, and changes the directory to the specified cwd above, but I have no idea how to pass text into the cmd window from Python.
I have tried passing it as a string argument within the subprocess.Popen brackets, I have also tried assigning PIPE to the stdin and stdout with not much luck. I am familiar with simple coding from Uni, but I am not familiar with Python's syntax or scripting.
Any help is greatly appreciated.
One of your issues may be that cmd.exe doesn't exit once you've started it.
I was successful passing the /C argument to cmd.exe, which tell it to exit after processing the command.
This works:
from subprocess import Popen,PIPE
my_command = "dir *.log"
with Popen([r"C:\Windows\System32\cmd.exe","/C",my_command], stdout=PIPE, cwd=r'C:\Temp') as proc:
print(proc.stdout.read().decode().replace("\r\n","\n"))

Python Subprocess Popen Prepare Python Path under Linux

I am trying to prepare my pythonpath under Ubuntu via subprocess.Popen call, for another script. The call to Python estimateskeleton.py does work fine. However since it needs the python path to be prepared it doesn't work completely correct, since it can not find some other scripts which need to be imported. The export PYTHONPATH command did work with commands.getoutput. However with commands.getoutput the estimateskeleton script still doesn't work / can't find the other files which should be imported. My try to export PYTHONPATH via subprocess.Popen resulted in Error Number 2:
OSError: [Errno 2] No such file or directory
I couldn't find a proper solution with the search function. So I am hoping that one of the more advanced users of this board can help me
Best Regards
import subprocess as sub
import os
import commands
proc = sub.Popen(["export", "PYTHONPATH=\"${PYTHONPATH}:/media/sf_myFolder/Scripts/code/\""],
stdout=sub.PIPE,
stderr=sub.STDOUT)
print proc.communicate()[0]
proc2 = sub.Popen(["python", "estimateskeleton.py"],
stdout=sub.PIPE,
stderr=sub.STDOUT)
print proc2.communicate()[0]
your first Popen command would work without shell=True because export is a shell built-in.
However, that won't fix it, because the second process spawned by Popen is unaware of the previous variable set in a dead process.
So instead of running the first useless Popen, you could add your path to existing PYTHONPATH using os.putenv() like this:
os.putenv("PYTHONPATH",os.pathsep.join([os.getenv("PYTHONPATH",""),"/media/sf_myFolder/Scripts/code"]))
so your next python command is run with the added folder in PYTHONPATH

Why 'os.system' exits with return code 1?

I want to execute some adb commands from python script. But when i executed the following line
os.system('adb devices')
The cmd returns with 1 instead of 0. I also tried executing
os.popen('adb devices').read()
I am getting empty string. Please help me to solve this.
Note: I tried the same commands from command window and it was working fine. I also added the path of adb.exe to windows PATH environment variable.
According to Windows docs, you've got 1, because there was an error on your command.
Maybe use subprocess could be a better approach.
import subprocess
subprocess.check_output(
"adb devices",
stderr=subprocess.STDOUT,
shell=True)

Run a program in Python's subprocess module, with required files from Terminal

Is it feasible to run a program in Python's subprocess module, but with files from Terminal?
So I want to run the following program from within Python:
myProgram -a myArg
However, suppose that the above program requires the file myFile in the current directory, and it doesn't take the required file as an argument. So, if you run the above program in the directory where there is myFile, the program succeeds in processing. However, if you run it in the directory where there is NOT myFile, the execution fails.
And when I tried to execute the program from within Python's subprocess.Popen(), with shell=True, the program doesn't work and it looks like the reason it failed is the program wasn't able to read myFile when executed from within Python.
So, is there any way to run it successfully from within Python?
subprocess.Popen('myProgram -a myArg', cwd='/folder/with_myFile/')
Similar Question: Python specify popen working directory via argument

Categories

Resources