Need Simple Way To Determine If File Is Executable
Tim Daneliuk
tundra at tundraware.com
Mon Dec 18 11:41:36 EST 2006
Gabriel Genellina wrote:
> On 17 dic, 19:21, "Roger Upole" <rup... at hotmail.com> 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.
>> On windows, you can use win32file.GetBinaryType to check if a file is actually
>> a binary executable.
>
> A similar function exists on Linux too. But even if a file has the
> right file format, if it does not have the execute bit set, won't run.
> And you could set that bit on a JPG image too - and nothing good would
> happen, I presume. So one must determine first what means "the file is
> executable".
>
Well... sure, but that isn't the point. Here is the problem I was
trying to solve:
I wrote and maintain the 'twander' cross-platform file browser:
http://www.tundraware.com/Software/twander/
I was working on a new release and wanted to add file associations
to it. That is, if the user selected a file and double clicked or
pressed Enter, I wanted the following behavior (in the following
steps, "type" means nothing more than "a file whose name ends with
a particular string"):
1) If an association for that file type exists, run the associated program.
2) If an association for that file type does not exist:
a) If the file is not "executable", see if there is a "default"
association defined and run that program if there is.
b) If the file *is* "executable", run it.
So ... all I really needed to know is whether or not the OS thinks the
file is executable. Obvious - and this is true on most any system -
you can create the situation where the file appear executable from
the OS's point of view, but it is not actually. But this is a pathology
that no application should really be expected to cope with...
--
----------------------------------------------------------------------------
Tim Daneliuk tundra at tundraware.com
PGP Key: http://www.tundraware.com/PGP/
More information about the Python-list
mailing list