Command prompt not shown when running Python script with subprocess on Windows

ps16thypresenceisfullnessofjoy at gmail.com ps16thypresenceisfullnessofjoy at gmail.com
Wed May 28 16:46:40 EDT 2014


Thank you for your replies. I tried what you suggested in your second post and it worked.

That was actually a mistake in the app_list.xml file. As you said:

<app name="LibreOffice Writer">%ProgramFiles%\LibreOffice 4\program\swriter.exe "C:\Users\Timothy\Documents\myfile.odt"</app>

should instead be:

<app name="LibreOffice Writer">"%ProgramFiles%\LibreOffice 4\program\swriter.exe" "C:\Users\Timothy\Documents\myfile.odt"</app>

I just made that file as a sample, and didn't actually test it.

My "shlex dance" has nothing to do with that, though. A few examples from the interactive interpreter should explain why I am doing it (I used raw strings in these examples so that I wouldn't need to escape the backslashes):

>>> import shlex
>>> shlex.split(r'C:\Users\Timothy\Documents\Python\myscript.py')
['C:UsersTimothyDocumentsPythonmyscript.py']
>>> shlex.split(r'C:\\Users\\Timothy\\Documents\\Python\\myscript.py')
['C:\\Users\\Timothy\\Documents\\Python\\myscript.py']
>>> shlex.split(r'C:\Users\Timothy\Documents\Python\myscript.py', posix=False)
['C:\\Users\\Timothy\\Documents\\Python\\myscript.py']

The first example shows that single backslashes get removed. The second example shows that double backslashes are preserved intact. The third example shows that if posix=False, single backslashes are converted to double backslashes. None of these three behaviors are acceptable to correctly parse a Windows path, which is why I am doing what I am to work around the issue.

I think I'll use PyWin32 as you suggested if it's available on the user's system. If it's not, the user will just be required to prefix path-to-script.py with python.exe. (When I first asked my question, I was thinking I might just need to pass a certain STARTUPINFO flag to subprocess.Popen, but it looks like that's not the solution.)

-- Timothy



More information about the Python-list mailing list