How to detect if a file is executable on Windows?

Stephane Wirtel stephane at wirtel.be
Tue Feb 19 08:47:55 EST 2019


And based on a determinist solution?

Yes, you can check the extension but sometimes I prefer to be sure.

For example, the API of Win32 has the GetTypeBinaryW function, this one
could be used but I need to create a mapping, with CPython api, ctypes
or CFFI.

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

But I wanted an elegant and minimalist solution.

Thank you for your proposal.

Have a nice day,

Stéphane

On 02/18, Igor Korot wrote:
>Hi,
>
>On Tue, Feb 19, 2019 at 7:26 AM Stephane Wirtel <stephane at wirtel.be> wrote:
>>
>> Hi all,
>>
>> on Linux/Unix/BSD, we can detect if a file is an executable with
>> os.access(path, os.X_OK) but this is not the case on Windows.
>>
>> Do you have an idea?
>
>Check file extension?
>
>Thank you.
>
>>
>> Have a nice day,
>>
>> Stéphane
>>
>> --
>> Stéphane Wirtel - https://wirtel.be - @matrixise
>> --
>> https://mail.python.org/mailman/listinfo/python-list

-- 
Stéphane Wirtel - https://wirtel.be - @matrixise


More information about the Python-list mailing list