Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need Simple Way To Determine If File Is Executable

Reply
Thread Tools

Need Simple Way To Determine If File Is Executable

 
 
Tim Daneliuk
Guest
Posts: n/a
 
      12-14-2006
I have a program wherein I want one behavior when a file is set as executable
and a different behavior if it is not. Is there a simple way to determine
whether a given named file is executable that does not resort to all the
lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?

Thanks,
----------------------------------------------------------------------------
Tim Daneliuk http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP Key: http://www.tundraware.com/PGP/
 
Reply With Quote
 
 
 
 
John McMonagle
Guest
Posts: n/a
 
      12-14-2006
Tim Daneliuk wrote:
> I have a program wherein I want one behavior when a file is set as executable
> and a different behavior if it is not. Is there a simple way to determine
> whether a given named file is executable that does not resort to all the
> lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?
>


os.access(pathToFile, os.X_OK)

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-15-2006
At Thursday 14/12/2006 19:21, John McMonagle wrote:

> > I have a program wherein I want one behavior when a file is set

> as executable
> > and a different behavior if it is not. Is there a simple way to determine
> > whether a given named file is executable that does not resort to all the
> > lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?
> >

>
>os.access(pathToFile, os.X_OK)


That won't work on Windows.

You have to define what do you mean by "a file is set as executable"
on Windows.
a.exe is executable and nobody would discuss that. I can supress the
extension and type simply: a, on the command line, and get a.exe
executed. Same for a.com
What about a.bat? cmd.exe is executed and runs the batch file. I can
even omit the extension. Is a.bat executable then?
What about a.py? Another process starts and handles the file
(python.exe). Is a.py executable then?
I can type a.mdb on the command prompt and launch an Access
application. Is a.mdb executable then?
If I type a.doc on the command prompt, Word is executed and opens
that file. Is a.doc executable then?

The answer may be so narrow to just consider .exe .com and a few
more, or so broad to consider all things that os.startfile can handle
without error.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      12-15-2006
[Tim Daneliuk]
> I have a program wherein I want one behavior when a file is
> set as executable and a different behavior if it is not. Is
> there a simple way to determine whether a given named file is
> executable that does not resort to all the lowlevel ugliness
> of os.stat() AND that is portable across Win32 and *nix?


I'm fairly certain the answer is no. What follows is a
relatively low-level and certainly not portable discussion.

The last couple of times this question came up on the list
I looked into the implementation and experimented a bit
but in short I would say that os.stat / os.access were
near enough useless for determining executablility under
Windows. That's not down to Python as such; it's simply
passing back what the crt offers.

Of course that raises the slightly wider issue of: should
the Python libs do more than simply call the underlying
crt especially when that's known to give, perhaps misleading
results? But I'm in no position to answer that.

I suggest that for Windows, you either use the PATHEXT
env var and determine whether a given file ends with
one of its components. Or -- and this depends on your
definition of executable under Windows -- use the
FindExecutable win32 API call (exposed in the win32api
module of pywin32 and available via ctypes) which will
return the "executable" for anything which has an
association defined. So the "executable" for a Word
doc is the winword.exe program. The "executable" for
an .exe is itself.

TJG

 
Reply With Quote
 
Tim Daneliuk
Guest
Posts: n/a
 
      12-15-2006
Tim Golden wrote:
> [Tim Daneliuk]
>> I have a program wherein I want one behavior when a file is
>> set as executable and a different behavior if it is not. Is
>> there a simple way to determine whether a given named file is
>> executable that does not resort to all the lowlevel ugliness
>> of os.stat() AND that is portable across Win32 and *nix?

>
> I'm fairly certain the answer is no. What follows is a
> relatively low-level and certainly not portable discussion.
>
> The last couple of times this question came up on the list
> I looked into the implementation and experimented a bit
> but in short I would say that os.stat / os.access were
> near enough useless for determining executablility under
> Windows. That's not down to Python as such; it's simply
> passing back what the crt offers.
>
> Of course that raises the slightly wider issue of: should
> the Python libs do more than simply call the underlying
> crt especially when that's known to give, perhaps misleading
> results? But I'm in no position to answer that.
>
> I suggest that for Windows, you either use the PATHEXT
> env var and determine whether a given file ends with
> one of its components. Or -- and this depends on your
> definition of executable under Windows -- use the
> FindExecutable win32 API call (exposed in the win32api
> module of pywin32 and available via ctypes) which will
> return the "executable" for anything which has an
> association defined. So the "executable" for a Word
> doc is the winword.exe program. The "executable" for
> an .exe is itself.
>
> TJG
>


This seems to work, at least approximately:

os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH

It probably does not catch every single instance of something
that could be considered "executable" because this is a sort
of fluid thing in Windows (as you point out).

--
----------------------------------------------------------------------------
Tim Daneliuk (E-Mail Removed)
PGP Key: http://www.tundraware.com/PGP/
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      12-16-2006
Tim Daneliuk <(E-Mail Removed)> wrote:
>
>This seems to work, at least approximately:
>
> os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH
>
>It probably does not catch every single instance of something
>that could be considered "executable" because this is a sort
>of fluid thing in Windows (as you point out).


This will tell you that "x.exe" is executable, even if "x.exe" contains
nothing but zeros.

On the other hand, I'm not convinced that any other solution is better.
--
Tim Roberts, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-16-2006
On 16 dic, 04:47, Tim Roberts <(E-Mail Removed)> wrote:
> > os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH


>This will tell you that "x.exe" is executable, even if "x.exe" contains
> nothing but zeros.


Isn't the same with any other recipe, portable or not? Unless the OS
actually tries to load and examine the file contents, which the OS's
I'm aware of, don't do.

--
Gabriel Genellina

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      12-17-2006
"Gabriel Genellina" <(E-Mail Removed)> wrote:

>On 16 dic, 04:47, Tim Roberts <(E-Mail Removed)> wrote:
>> > os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH

>
>>This will tell you that "x.exe" is executable, even if "x.exe" contains
>> nothing but zeros.

>
>Isn't the same with any other recipe, portable or not? Unless the OS
>actually tries to load and examine the file contents, which the OS's
>I'm aware of, don't do.


Yes, of course, you're right. I was about to delve into a philosophical
discussion about the difference in handling this between Linux and Windows,
but they're both just conventions. One is based on an arbitrary flag, one
is based on a file extension. Contents are irrelevant.
--
Tim Roberts, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Roger Upole
Guest
Posts: n/a
 
      12-17-2006

Gabriel Genellina wrote:
> On 16 dic, 04:47, Tim Roberts <(E-Mail Removed)> wrote:
>> > os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH

>
>>This will tell you that "x.exe" is executable, even if "x.exe" contains
>> nothing but zeros.

>
> Isn't the same with any other recipe, portable or not? Unless the OS
> actually tries to load and examine the file contents, which the OS's
> I'm aware of, don't do.
>
> --
> Gabriel Genellina
>


On windows, you can use win32file.GetBinaryType to check if a file is actually
a binary executable.

Roger




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Tim Daneliuk
Guest
Posts: n/a
 
      12-17-2006
Roger Upole wrote:
> Gabriel Genellina wrote:
>> On 16 dic, 04:47, Tim Roberts <(E-Mail Removed)> wrote:
>>>> os.stat(selected)[ST_MODE] & (S_IXUSR|S_IXGRP|S_IXOTH
>>> This will tell you that "x.exe" is executable, even if "x.exe" contains
>>> nothing but zeros.

>> Isn't the same with any other recipe, portable or not? Unless the OS
>> actually tries to load and examine the file contents, which the OS's
>> I'm aware of, don't do.
>>
>> --
>> Gabriel Genellina
>>

>
> On windows, you can use win32file.GetBinaryType to check if a file is actually
> a binary executable.
>
> Roger
>


Yabut ... what about things like batch files? Does it return them
as executable as well?


--
----------------------------------------------------------------------------
Tim Daneliuk (E-Mail Removed)
PGP Key: http://www.tundraware.com/PGP/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine executable image file type hoenes1 Windows 64bit 1 12-02-2009 01:09 PM
Determine if a file is executable on the OS Philipp Java 2 10-16-2007 11:44 AM
what is fast dynamically linked executable or statically linked executable ?how to decide? pratap C Programming 20 03-07-2007 04:46 AM
How can I run a c executable in pwd ,while the executable is in some other directory vishsid3@gmail.com C Programming 15 08-21-2006 06:04 AM
how to determine if jar or class file is executable kaith Java 2 08-16-2003 07:46 AM



Advertisments