我有一些关于在Python中设置最大运行时间的问题。事实上,我想使用pdfminer将PDF文件转换为.txt。问题是,很多时候,有些文件无法解码,并且需要非常长的时间。所以我想设置time.time()
,将每个文件的转换时间限制为20秒。此外,我在Windows下运行,因此无法使用信号功能
我成功地用pdfminer.convert_pdf_to_txt()
运行了转换代码(在我的代码中它是“c”),但我无法将time.time()
集成到while循环中。在我看来,在下面的代码中,while循环和time.time()
不起作用
总之,我想:
将PDf文件转换为.txt文件
每次转换的时间限制为20秒。如果超时,抛出异常并保存一个空文件
将所有txt文件保存在同一文件夹下
如果存在任何异常/错误,仍然保存文件,但内容为空
以下是当前代码:
import converter as c
import os
import timeit
import time
yourpath = 'D:/hh/'
for root, dirs, files in os.walk(yourpath, topdown=False):
for name in files:
t_end = time.time() + 20
try:
while time.time() < t_end:
c.convert_pdf_to_txt(os.path.join(root, name))
t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])
g = str(a.split("\\")[1])
with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
print "yes"
if time.time() > t_end:
print "no"
with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
newfile.write("")
except KeyboardInterrupt:
raise
except:
for name in files:
t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])
g = str(a.split("\\")[1])
with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
newfile.write("")
你的方法不对
定义结束时间,如果当前时间戳低于结束时间戳(将始终为^{),则立即进入
while
循环。因此while
循环被输入,您被困在转换函数中我建议使用
signal
模块,它已经包含在Python中。它允许您在n
秒后退出函数。一个基本的例子可以在this Stack Overflow answer中看到您的代码如下所示:
只是为了将来:四个空格缩进,没有太多空格;)
相关问题 更多 >
编程相关推荐