需要简单的方法来确定文件是否可执行 [英] Need Simple Way To Determine If File Is Executable

查看:79
本文介绍了需要简单的方法来确定文件是否可执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中我想要一个行为,当一个文件被设置为可执行时

和一个不同的行为,如果不是。是否有一种简单的方法来确定给定的命名文件是否是可执行的,而不是诉诸于os.stat()的所有低级丑陋和可移植的文件。 Win32和* nix?


谢谢,

----------------------- -------------------------------------------------- ---

Tim Daneliuk tu****@tundraware.com

PGP密钥: http://www.tundraware.com/PGP/

解决方案

Tim Daneliuk写道:


我有一个程序,其中我想要一个行为当一个文件被设置为可执行时

和一个不同的行为,如果不是。是否有一种简单的方法来确定给定的命名文件是否是可执行的,而不是诉诸于os.stat()的所有低级丑陋和可移植的文件。 Win32和* nix?



os.access(pathToFile,os.X_OK)


-

此邮件已通过MailScanner扫描病毒和

危险内容,并且

被认为是干净的。


在2006年12月14日星期四19:21,John McMonagle写道:


我有一个程序,其中我想要一个文件设置时的行为



作为可执行文件


和不同的行为如果它不是。是否有一种简单的方法来确定给定的命名文件是否是可执行的,而不是诉诸于os.stat()的所有低级丑陋和可移植的文件。 Win32和* nix?


os.access(pathToFile,os.X_OK)



这不适用于Windows。


您必须在Windows上定义文件被设置为可执行文件是什么意思



a.exe是可执行文件,没有人会讨论。我可以压缩

扩展名并简单地输入:a,在命令行上,并执行a.exe

执行。同样适用于a.com

a.bat怎么样?执行cmd.exe并运行批处理文件。我可以

甚至省略扩展名。那么是a.bat可执行文件吗?

a.py怎么样?另一个进程启动并处理文件

(python.exe)。是a.py可执行文件吗?

我可以在命令提示符下键入a.mdb并启动Access

应用程序。那么是a.mdb可执行文件吗?

如果我在命令提示符下键入a.doc,则执行Word并打开

该文件。那么是a.doc可执行文件吗?


答案可能非常狭窄,只考虑.exe .com和一些

更多或者更广泛的考虑os.startfile可以处理的所有东西

没有错误。

-

Gabriel Genellina

Softlab SRL


__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar


[Tim Daneliuk]


我有一个程序,其中我想要一个行为,当一个文件是

设置为可执行文件,如果不是,则设置为不同的行为。是

有一个简单的方法可以确定给定的命名文件是否是
可执行文件,它不会诉诸于所有低级丑陋的操作。 stat()和可以在Win32和* nix上移植吗?



我很确定答案是否定的。以下是

相对较低级别,当然也不是便携式讨论。


最近几次这个问题出现在名单上

我查看了实现并尝试了一下

但简而言之我会说os.stat / os.access是

足够无用来确定可执行性在

Windows下。这不是因为Python;这简直就是回报crt提供的东西。


当然这会引发一个稍微宽泛的问题:应该

Python库不仅仅是简单地调用底层的
crt,尤其是那些已知的,或许误导

的结果?但是我无法回答这个问题。


我建议对于Windows,你要么使用PATHEXT

env var并确定是否给定文件结尾是

其中一个组件。或者 - 这取决于您在Windows下的可执行文件的定义 - 使用

FindExecutable win32 API调用(在win32api中公开

pywin32的模块,可以通过ctypes获得)

返回可执行文件对于任何定义了

关联的东西。所以可执行是指可执行。换一个字

doc是winword.exe程序。 可执行的是指可执行的。对于

和.exe本身就是。


TJG


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****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

解决方案

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(pathToFile, os.X_OK)

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


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(pathToFile, 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


[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


这篇关于需要简单的方法来确定文件是否可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆