Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'win32': 0.03; 'interpreter': 0.04; '21,': 0.07; 'executable': 0.07; 'filename': 0.07; 'though:': 0.07; 'api': 0.09; 'extension.': 0.09; 'if,': 0.09; 'sep': 0.09; 'windows,': 0.09; 'extension': 0.13; 'extensions': 0.13; 'copied.': 0.16; 'echo': 0.16; 'executables': 0.16; 'fine.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'module?': 0.16; 'separator,': 0.16; 'subject:which': 0.16; 'yup,': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'thu,': 0.17; 'tries': 0.17; 'typing': 0.17; 'windows': 0.19; 'putting': 0.20; 'received:209.85.214.174': 0.21; 'programming': 0.23; 'split': 0.23; "i've": 0.23; 'command': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'first,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'there.': 0.28; 'actual': 0.28; 'chris': 0.28; 'fine': 0.28; 'behaviour': 0.29; 'subject:like': 0.29; "i'm": 0.29; 'fri,': 0.30; 'function': 0.30; 'file': 0.32; 'directory,': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'path': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'really': 0.36; 'but': 0.36; 'subject: (': 0.36; 'enough': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'between': 0.63; '20,': 0.65; 'hello!': 0.65; 'different.': 0.84; 'same,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=fYg4lHOlDFFPkxJ/s06d4cJxdYQ2e2+H3wi2Z3DZQUo=; b=rDpIu3Ogyoa103ngm9I1X3Y0/xhIAppfoTXeS0d8dfeZbCbwueT8s1ONd/bUJkV+ub Y+IGhaSsQxcsf0+N0WVPX0uuPM3j62BFTyYJm5lkJ/r9jjbPjZHZbn5yrD79fzeri1mu +1SlJO0/Lf3iaPem8uNhS4pPie9WV6qTWwbXq2F8dsqJuVLaUZ7/aU2XvajPfPCocPUt x1JTDlOGPcur4XCVUM40NdHdlBVoElv052w4kGDEPzuAPTqsWsyH3xQDJaXTqnriSrTd 8+JlQga9jYIMtaT/YZKhYmYF+9NhvChBfPvIS8498Yx7GO8tobMHm1Ebh2nsIEKjEyuO 6AVQ== MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 21 Sep 2012 09:04:06 +1000 Subject: Re: portable way of locating an executable (like which) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348182248 news.xs4all.nl 6981 [2001:888:2000:d::a6]:54063 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29581 On Fri, Sep 21, 2012 at 8:32 AM, Ian Kelly wrote: > On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico wrote: >> os.sep is the directory separator, but os.pathsep may be what you >> want. Between that and os.getenv('path') you can at least get the >> directories. Then on Windows, you also need to check out >> os.getenv('pathext') and split _that_ on the semicolon, and try each >> of those as a file extension. I'm not sure whether or not Windows will >> add extensions from pathext if one is given on the command line - for >> instance, if typing "foo.exe" will search for "foo.exe.bat" - but the >> basics are there. > > Easy enough to test: > > C:\>echo echo hello! > foo.exe.bat > > C:\>foo.exe > hello! > > Yup, it does. It looks like it tries it without the extension first, though: > > C:\>copy c:\windows\notepad.exe foo.exe > 1 file(s) copied. > > C:\>foo.exe > [starts notepad] Well, at least it's consistent. Makes your PATH extremely sensitive, though, easy for anyone to inject executables into it. But then, you can already do that by putting them in the current directory, so that's not really any different. Jason's solution looks fine apart from the PATHEXT requirement, so if you know you have the full filename and you don't care if the actual command interpreter will do exactly the same, that'll do you fine. Is this something that might want to be a function in the os module? Particularly so if, as I suspect there might be, there's a Win32 API function that precisely replicates the behaviour of executable invocation. A while since I've done much Windows programming but I think there's a SearchPath function? ChrisA