Need Simple Way To Determine If File Is Executable

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


Tim Roberts <timr at probo.com> schrieb

> "Gabriel Genellina" <gagsl-py at yahoo.com.ar> wrote:
> 
>>On 16 dic, 04:47, Tim Roberts <t... at probo.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.
> 
> 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. 

No, they aren't! Try this:

[lunar at nargond]-[10:24:44] >> ~/test
--> dd if=/dev/zero of=test.sh count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 6.5e-05 seconds, 7.9 MB/s

[lunar at nargond]-[10:24:46] >> ~/test
--> chmod a+x test.sh

[lunar at nargond]-[10:24:55] >> ~/test
--> ./test.sh
bash: ./test.sh: cannot execute binary file

A file containing only zeros isn't executed...

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



More information about the Python-list mailing list