【发布时间】:2014-05-28 13:07:03
【问题描述】:
我目前正在执行一项任务,我需要使用 pefile 模块迭代多个可执行文件,代码如下所示
while True:
try:
for filename in glob.iglob('C:\Documents and Settings\Zha\Desktop\PE benign\*.exe'):
pe = pefile.PE(filename)
print '%x' % pe.FILE_HEADER.NumberOfSections
except:
pass
我使用 try 和 except 的目的是为了克服错误引发,只要可执行文件引发错误,其中 NT 标头给出无效签名,因为如果我不使用 try 和 except,代码将停止在它发现可执行文件无效的点NT 头签名
这就是我不使用 try 和 except 时消息的样子
PEFormatError: 'Invalid NT Headers signature.'
但是使用上面的代码会导致死循环,请问有什么办法可以解决吗?
【问题讨论】:
-
那么不要使用无限循环吗?将您的
trycloser 移至异常;仅将其放在pefile.PE()调用周围。 -
@TheGameDoctor 请注意,您的字符串
'C:\Documents and Settings\Zha\Desktop\PE benign\*.exe'错误,如` is escape character. Correct it to'C:\\Documents and Settings\\Zha\\Desktop\\PE 良性\*.exe'` 或到r'C:\Documents and Settings\Zha\Desktop\PE benign\*.exe'或使用正斜杠'C:/Documents and Settings/Zha/Desktop/PE benign/*.exe'
标签: python python-2.7 exception exception-handling infinite-loop