473,836 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Simple Way To Determine If File Is Executable

I have a program wherein I want one behavior when a file is set as executable
and a different behavior if it is not. Is there a simple way to determine
whether a given named file is executable that does not resort to all the
lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?

Thanks,
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Dec 14 '06 #1
28 20727
Tim Daneliuk wrote:
I have a program wherein I want one behavior when a file is set as executable
and a different behavior if it is not. Is there a simple way to determine
whether a given named file is executable that does not resort to all the
lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?
os.access(pathT oFile, os.X_OK)

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Dec 14 '06 #2
At Thursday 14/12/2006 19:21, John McMonagle wrote:
I have a program wherein I want one behavior when a file is set
as executable
and a different behavior if it is not. Is there a simple way to determine
whether a given named file is executable that does not resort to all the
lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix?

os.access(path ToFile, os.X_OK)
That won't work on Windows.

You have to define what do you mean by "a file is set as executable"
on Windows.
a.exe is executable and nobody would discuss that. I can supress the
extension and type simply: a, on the command line, and get a.exe
executed. Same for a.com
What about a.bat? cmd.exe is executed and runs the batch file. I can
even omit the extension. Is a.bat executable then?
What about a.py? Another process starts and handles the file
(python.exe). Is a.py executable then?
I can type a.mdb on the command prompt and launch an Access
application. Is a.mdb executable then?
If I type a.doc on the command prompt, Word is executed and opens
that file. Is a.doc executable then?

The answer may be so narrow to just consider .exe .com and a few
more, or so broad to consider all things that os.startfile can handle
without error.
--
Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Dec 15 '06 #3
[Tim Daneliuk]
I have a program wherein I want one behavior when a file is
set as executable and a different behavior if it is not. Is
there a simple way to determine whether a given named file is
executable that does not resort to all the lowlevel ugliness
of os.stat() AND that is portable across Win32 and *nix?
I'm fairly certain the answer is no. What follows is a
relatively low-level and certainly not portable discussion.

The last couple of times this question came up on the list
I looked into the implementation and experimented a bit
but in short I would say that os.stat / os.access were
near enough useless for determining executablility under
Windows. That's not down to Python as such; it's simply
passing back what the crt offers.

Of course that raises the slightly wider issue of: should
the Python libs do more than simply call the underlying
crt especially when that's known to give, perhaps misleading
results? But I'm in no position to answer that.

I suggest that for Windows, you either use the PATHEXT
env var and determine whether a given file ends with
one of its components. Or -- and this depends on your
definition of executable under Windows -- use the
FindExecutable win32 API call (exposed in the win32api
module of pywin32 and available via ctypes) which will
return the "executable " for anything which has an
association defined. So the "executable " for a Word
doc is the winword.exe program. The "executable " for
an .exe is itself.

TJG

Dec 15 '06 #4
Tim Golden wrote:
[Tim Daneliuk]
>I have a program wherein I want one behavior when a file is
set as executable and a different behavior if it is not. Is
there a simple way to determine whether a given named file is
executable that does not resort to all the lowlevel ugliness
of os.stat() AND that is portable across Win32 and *nix?

I'm fairly certain the answer is no. What follows is a
relatively low-level and certainly not portable discussion.

The last couple of times this question came up on the list
I looked into the implementation and experimented a bit
but in short I would say that os.stat / os.access were
near enough useless for determining executablility under
Windows. That's not down to Python as such; it's simply
passing back what the crt offers.

Of course that raises the slightly wider issue of: should
the Python libs do more than simply call the underlying
crt especially when that's known to give, perhaps misleading
results? But I'm in no position to answer that.

I suggest that for Windows, you either use the PATHEXT
env var and determine whether a given file ends with
one of its components. Or -- and this depends on your
definition of executable under Windows -- use the
FindExecutable win32 API call (exposed in the win32api
module of pywin32 and available via ctypes) which will
return the "executable " for anything which has an
association defined. So the "executable " for a Word
doc is the winword.exe program. The "executable " for
an .exe is itself.

TJG
This seems to work, at least approximately:

os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|S_IXOTH

It probably does not catch every single instance of something
that could be considered "executable " because this is a sort
of fluid thing in Windows (as you point out).

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Dec 15 '06 #5
Tim Daneliuk <tu****@tundraw are.comwrote:
>
This seems to work, at least approximately:

os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|S_IXOTH

It probably does not catch every single instance of something
that could be considered "executable " because this is a sort
of fluid thing in Windows (as you point out).
This will tell you that "x.exe" is executable, even if "x.exe" contains
nothing but zeros.

On the other hand, I'm not convinced that any other solution is better.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 16 '06 #6
On 16 dic, 04:47, Tim Roberts <t...@probo.com wrote:
os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|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.

--
Gabriel Genellina

Dec 16 '06 #7
"Gabriel Genellina" <ga******@yahoo .com.arwrote:
>On 16 dic, 04:47, Tim Roberts <t...@probo.com wrote:
os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|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.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 17 '06 #8

Gabriel Genellina wrote:
On 16 dic, 04:47, Tim Roberts <t...@probo.com wrote:
os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|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.

--
Gabriel Genellina
On windows, you can use win32file.GetBi naryType to check if a file is actually
a binary executable.

Roger


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '06 #9
Roger Upole wrote:
Gabriel Genellina wrote:
>On 16 dic, 04:47, Tim Roberts <t...@probo.com wrote:
>>> os.stat(selecte d)[ST_MODE] & (S_IXUSR|S_IXGR P|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.

--
Gabriel Genellina

On windows, you can use win32file.GetBi naryType to check if a file is actually
a binary executable.

Roger
Yabut ... what about things like batch files? Does it return them
as executable as well?
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Dec 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
7846
by: Sunner Sun | last post by:
Hi, all Since the OS look both ASCII and binary file as a sequence of bytes, is there any way to determine the file type except to judge the extension? Thank you!
2
1995
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The problem comes with the Data. The data is coming from a different application, and I am not 100% sure of...
17
2186
by: Nirjhar Oberoi | last post by:
Hi, I am new to Linux and wanted to know how to use GCC to Compile the Code written in C? I dont want to use EMacs or VI for my editor. Can you suggest a good IDE for linux for C Programming.. Does C Programming on windows environment and on linux environment differ??? Thank you.
3
2097
by: techtonik | last post by:
Hello, everyb. Does anybody know simple cross-platform method of probing if executable binary is available and launching it. Problem no.1: test if executable file is available I'll take windows platform as the most relevant in this case. os.access() doesn't handle env PATHEXT and can't detect if a given path would be executable or not. Here "executable" means file that
0
9812
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10828
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10534
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4444
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.