subprocess.Popen unexpected behavior - python-2.7

I inherited some Python code which uses subprocess.Popen to print a pdf file using Adobe Reader. The code has worked well for years running on Windows 7, I am getting it set up on Windows 10. For some reason the call to Popen starts an Adobe Reader process, but does not open the application. The code works as expected if I run it from the Python interpreter. But when it when it is running as a Windows process, Adobe Reader does not open, or print the pdf. I checked the command line parameters for the running process and it is invoked exactly the same as when run from the Python Interpreter. A code snippet is below.
printFileName = r'"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" /p /h /t '
printFileName = printFileName + filename
subprocess.Popen(printFileName)
filename is the full path to a .pdf file.
I'm not really sure where to even begin figuring this out so any pointers or suggestions on how to improve the question would be appreciated.

Related

Sublime Text 3 files not opening in command line/powershell

I’ve just finished Codecademy and am setting up Python (2.7) and Sublime Text 3 on my own computer using the Codecademy guide page. I can successfully perform simple operations (eg. Print “Hello world”) a) in Python and b) by using ctrl+b on text in my Sublime Text 3 editor page, which gives the output in the box at the bottom of the page.
I’m stuck/confused when it comes to running the Sublime Text 3 file through the command line and confused on the powershell vs. command line issue (I’m running Windows 10).
My command line does not display the $ sign in the codecademy page example (https://www.codecademy.com/articles/setup-python), so should I be entering the commands below through Powershell rather than the command line? If so, I don’t get the $ in Powershell either.
If I carry on regardless and try to change directories etc through Command Prompt or Powershell, I only get error messages and can't seem to run the Sublime Text 3 file.
I’m also not clear on whether this is an issue about which directory I’m in. Running dir in both the Command prompt and Powershell returns the result: after I’ve opened Python, but lists all subfolders of C:\Users\my_name if I run it before opening Python. Does this mean that I need to save my Sublime Text files in some sort of subfolder of Python in order to be able to run them as above?
Or does it not matter that I’m not able to run the Sublime Text 3 files through Python directly and I should just stick to doing so through Sublime Text 3 itself? Will this limit me later on?
Thanks for your help
BC89

QProcess Wrong Behavior

My application runs different bash files when i run my application under QTCreator everything works fine but when i run my application directly i cant read the QProcess output . even when i run my application via Terminal it works fine , so where is the problem ?
i'm using QT 5.7 / OSX Platform
here is my code
QProcess proc ;
proc.start(QCoreApplication::applicationDirPath() + "/check.sh");
proc.waitForFinished();
QString output = QString(proc.readAll());
qDebug() << output ;
There are some possibilities you should investigate
Can you confirm that scripts are running though when you run it standalone?
QProcess always was a little skittish about creating processes when supplied scripts, depending on platform. Does script have shebang in it? Does it match the shell you're running your program from? You may need to create process based on shell, supplying script's file name as a parameter.
QProcess::readAll() May return nothing if output buffer wasn't flushed by the process. Outputting EOL at end would force the flush.

Python subprocess is unstable on Windows 10?

p = subprocess.Popen([executable_file])
This is only code that I am using to run the python subprocess. However, it has unknown issue that cause my program cannot open the executable file as expected.
executable_file is one file link (PATH) that locate executable program.
Ex. C:\Users\SharkIng\Dev\WhatEverSystem\Builds\Windows\program-123456789-123456789.exe
The python subprocess.Popen should run the program. However, sometime it works and sometime it is not (I did not change any code between this two situation)
OS: Windows 10
Python: 2.7.*
Error: [Error 2] The system cannot find the file specified
BUT: It is working if you manually run subprocess.Popen([executable_file_path_string]) (with same file and path) it work.
WHY this happen?? Is that because some problem with Windows Python? or it is because my setting mess me up?
UPDATE: It is not some reason such as NOT FULL PATH. I can have it working with exact same code. If I ran same code in a python shell, by typing each line of code. It works. But if I put exact same code in a .py file and run it with python file.py, it showing the error
UPDATE 2: Another team member have same error with Windows 7 and Python 2.7.* This code used to work this morning. AGAIN I didn't change anything. That is way I am asking if it is unstable.
There is no problem with subprocess. If you want to run a specified executable, you need to give the full path to that file. The most likely reason you're seeing variable behavior is that sometimes you're in the directory with the .exe, and other times you're not.

command prompt appears than immediately disappears

I encountered this problem after following a python tutorial on Youtube about creating text files. The instructor had us type in the following code to start:
def createFile(dest):
print dest
if__name__ == '__main__':
createFile('ham')
raw_input('done!')
We had created a folder on the desktop to store the file 'ham' in. But when I double clicked on 'ham' the command prompt window popped on then in a flash it popped off. I am an obvious beginner and I don't know what is going on. Can anyone explain this to me?
You can open command prompt then navigate to python interpreter
directory and run your program by typing python /diretory/to/your/program.py for
example if you have a program named test.py in the directory c:/python and you want to
run it and you have python interpreter installed in C:/python2.x/ directory
then you should open command prompt and type in
cd c:\python2.x\
then you should type
python c:/python/test.py
and perfectly it will work
showing you your program output .

How to open text file in notepad with cmd window in background?

I want to open a text file without opening cmd window in the background. I have tried:
webbrowser.open('file.txt')
but it crashes ArcGIS so I tried following:
os.system('file.txt')
it opens text file without crashing ArcGIS but cmd window remains in the background and goes away when I close text file.
It is more of a display choice question and just checking if there is any suggestion to avoid cmd window in the background.
I don't know if this is what you want, but maybe you should create .bat file (something like here) and run this with Python subprocess.
Save your script with a .pyw extension and the console window won't appear.
From the Python documentation :
On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
You need to modify the program that calls the "add-in script" to run it with pythonw.exe (and not python.exe which is the default).