Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Windows': 0.02; 'example:': 0.03; 'offline': 0.03; 'subject:not': 0.03; 'skip:[ 20': 0.04; 'api.': 0.05; 'argument': 0.05; 'subject:Python': 0.06; 'args': 0.07; 'paths': 0.07; 'assuming': 0.09; 'messing': 0.09; 'outlined': 0.09; 'skip:% 20': 0.09; 'subject:script': 0.09; 'api': 0.11; 'python': 0.11; 'thread': 0.14; 'windows': 0.15; 'above?': 0.16; 'although,': 0.16; 'be:': 0.16; 'dance,': 0.16; 'earlier.': 0.16; 'exe': 0.16; 'formatted': 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'golden': 0.16; 'instead:': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'nail': 0.16; 'pywin32': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'shortcut': 0.16; 'subject: \n ': 0.16; 'subject:when': 0.16; 'tjg': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'file,': 0.19; 'skip:p 40': 0.19; 'split': 0.19; 'entered': 0.20; 'command': 0.22; 'import': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; 'package.': 0.24; 'specify': 0.24; "haven't": 0.24; "i've": 0.25; 'skip:" 30': 0.26; 'suggested': 0.26; 'this:': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; 'xml': 0.29; 'along': 0.30; "i'm": 0.30; 'code': 0.31; '(although': 0.31; 'ctypes': 0.31; 'directly,': 0.31; 'invoke': 0.31; 'libraries': 0.31; 'post.': 0.31; 'file': 0.32; 'figure': 0.32; 'front': 0.32; 'quite': 0.32; 'open': 0.33; 'comment': 0.34; 'could': 0.34; 'subject:with': 0.35; 'something': 0.35; 'case,': 0.35; 'usual': 0.35; 'but': 0.35; 'dance': 0.36; 'done': 0.36; "didn't": 0.36; "i'll": 0.36; 'possible': 0.36; 'should': 0.36; 'so,': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'though,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'users': 0.40; 'read': 0.60; 'around.': 0.60; 'up,': 0.60; 'break': 0.61; 'simple': 0.61; "you're": 0.61; 'further': 0.61; 'first': 0.61; 'making': 0.63; 'information': 0.63; 'provide': 0.64; 'more': 0.64; 'temporary': 0.65; 'skip:w 30': 0.69; 'from:addr:mail': 0.83; 'associations': 0.84; 'env': 0.84; 'skip:4 20': 0.91 Date: Wed, 28 May 2014 20:46:25 +0100 From: Tim Golden User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Command prompt not shown when running Python script with subprocess on Windows References: <29c26176-b573-4ac9-bf41-a18a53d7dfb9@googlegroups.com> <53856F53.5010303@timgolden.me.uk> In-Reply-To: <53856F53.5010303@timgolden.me.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401306385 news.xs4all.nl 2948 [2001:888:2000:d::a6]:54680 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72183 On 28/05/2014 06:08, Tim Golden wrote: > On 28/05/2014 00:01, ps16thypresenceisfullnessofjoy@gmail.com wrote: > >> I want users to be able to enter paths in the XML file exactly the >> way they would be entered in a Windows shortcut. Since it is possible >> to make a Windows shortcut for path-to-script.py without the >> python.exe in front of it and have it open in its own command prompt, >> I want to be able to do the same thing in my XML file, but this is >> what I cannot figure out. > > Anything done through shortcuts is making use of the Windows Shell API. > To mimic that behaviour, you could try using that; in this case, > ShellExecute[Ex]. For simple purposes, this is exposed in Python as > os.startfile. If you need more control, you'd have to use the API call > directly, either via ctypes or via the pywin32 libraries under the > win32com.shell package. > > To mimic the behaviour exactly (if that is a requirement), you could > actually create a temporary shortcut with the desired information and > invoke it via os.startfile. > > I haven't followed the thread (and I'm offline at the moment) so I'll > wait until I've seen it before I comment on the shlex.split / \\ dance > above. On the surface, though, I'm not sure what it's achieving. [All > right, I didn't wait :)]. I've just read the original post. My answer above isn't quite on the nail (although it might still be useful). If you're prepared to use the pywin32 libraries, then you could use win32api.FindExecutable to provide a first argument to the Popen constructor, followed by the others in your data. Something like this: import subprocess import win32api # ... for app_path in app_paths: args = ... split ... _, exe = win32api.FindExecutable(args[0]) if exe != os.path.abspath(args[0]): args = [exe] + args subprocess.call(args) As to the shlex dance, I *think* you're trying to break the command line up, expand any env vars, and then pass it along to Popen as above? If your data were formatted as though for a Windows command-line, ie with the paths double-quoted if they contain spaces, then shlex should do the right thing by it without any further messing around. So, if this example: %ProgramFiles%\LibreOffice 4\program\swriter.exe "C:\Users\Timothy\Documents\myfile.odt" were instead: "%ProgramFiles%\LibreOffice 4\program\swriter.exe" "C:\Users\Timothy\Documents\myfile.odt" then the shlex dance would just be: args = [os.path.expandvars(i) for i in shlex.split(app_path)] Although, assuming associations were set up in the usual way, the code I outlined above to use FindExecutable would cope with this without the need to specify the swriter.exe. As would the os.startfile approach I suggested earlier. TJG