How to detect if a file is executable on Windows?

MRAB python at mrabarnett.plus.com
Tue Feb 19 14:07:12 EST 2019


On 2019-02-19 18:31, Grant Edwards wrote:
> On 2019-02-19, Chris Angelico <rosuav at gmail.com> wrote:
>> On Wed, Feb 20, 2019 at 5:05 AM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>>>
>>> On 19 Feb 2019 13:58:18 GMT, jureq <jureq at nowhere.no> declaimed the
>>> following:
>>>
>>> >> I could also use the 2 first bytes of a file and determine if the file
>>> >> is a binary because on Windows, the executable files start with b'MZ'.
>>> >
>>> >Is .bat executable?
>>>
>>>         Or any of the extensions on
>>> PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
>>
>> I guess you have to define the question better for Windows, since
>> there's no single definition of "executable". If you mean "typing just
>> the base name of this file at the shell will result in it being run",
>> then PATHEXT is the correct answer. If you mean "this thing is
>> actually inherently executable", then you probably want to check if it
>> begins MZ, but that's not certain (COM files still seem to be
>> supported, and they have no header whatsoever). If you mean
>> "double-clicking this thing will run it", I think there are tools that
>> allow you to do the registry lookup conveniently to see if something's
>> associated.
> 
> FWIW, I've noticed that afer downloading a .exe under Linux and
> scp'ing the file to a Windows machine, it wont run when double-clicked
> until I fire up a Cygwin shell and do a
> 
>     chmod +x <whatever>.exe
> 
> [I assume there's native Windows point-and-grunt means for doing that
> as well.]
> 
> So, in addition to the suffix and associations, there's some sort of
> file-system meta-data that determines whether a file is "executable"
> in some contexts.
> 
I've never heard of a Windows equivalent of "chmod +x". If I copy the 
contents of a .exe into a new file and give it a .exe extension, it just 
works.



More information about the Python-list mailing list