在Windows中使用sigalmar逻辑

2024-05-14 11:16:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在两种格式之间进行文件对话。如果时间太长,我想跳过该文件并继续。下面是我正在使用的脚本:

import time
from threading import Thread
from os.path import basename
import os, glob

# Custom exception class
class TimeoutException(Exception):
    pass


# Custom alarm handler
class Alarm(Thread):
    def __init__(self, timeout):
        Thread.__init__(self)
        self.timeout = timeout
        self.setDaemon(True)

    def run(self):
        time.sleep(self.timeout)
        raise TimeoutException
for folder, subfolders, filenames in os.walk(directory):
     if any([filename.endswith('.scad') for filename in filenames]):
         if filename.endswith('.scad'):
             os.chdir(folder)
             of = filename.replace('.scad', '.stl') # name of the outfile .stl
             # This try/except loop ensures that
             #   you'll catch TimeoutException when it's sent.
             try:
                 alarm = Alarm(5) # Create the timer. Once 5 seconds are over, a alarm will go off.
                 alarm.start() # Start the timer
                 os.system("\"convert.exe\" -o {} {}".format(of, filename))
             except TimeOutException:
                 print("Timed out for file name: {}".format("filename"))
             else:
                 del alarm # Reset the alarm

如何使脚本在Windows中工作?我在Linux中引用了sigalarm,但无法在windows中运行


Tags: 文件oftheimportself脚本foros

热门问题