Need Simple Way To Determine If File Is Executable

Sebastian 'lunar' Wiesner basti.wiesner at gmx.net
Wed Dec 20 04:19:52 EST 2006


Gabriel Genellina <gagsl-py at yahoo.com.ar> schrieb

> 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. 

Really? I don't think so. Afaik on Linux executable binary files need an
ELF header.

[lunar at nargond]-[10:15:43] >> ~/Bilder
--> chmod a+x VM-Background.png

[lunar at nargond]-[10:15:46] >> ~/Bilder
--> ./VM-Background.png
bash: ./VM-Background.png: cannot execute binary file

As you can see, binary files without such a header are not executed...

Bye
lunar

-- 
Freedom is always the freedom of dissenters.
        (Rosa Luxemburg)



More information about the Python-list mailing list