【问题标题】:How to loop in the try: exception?如何循环尝试:异常?
【发布时间】:2021-07-08 21:08:22
【问题描述】:

我正在提取一个 gz 文件,它给了我一个 errno,因为在我也在做的 scraping 中,浏览器还没有下载该文件,所以我想尝试一下,如果错误出现然后我让它等待一段时间并将其放回提取但我做错了,甚至没有接近诚实,我正在这样做:

    try:
     pass
    ## Download to folder and use whatever comes out
     file_downloaded = False
     while not file_downloaded:
         for file in os.listdir("tmp"):
             if file.endswith("csv.gz"):
                 fp = os.path.join('tmp', file)
                 logmsg = ("Extracting ", fp)
                 instalog.appendMessage('INFO', logmsg)
                 with gzip.open(fp, 'r') as f_in:
                     with open(newfile_path, 'wb') as f_out:
                         shutil.copyfileobj(f_in, f_out)
                         file_downloaded = True
             elif file.endswith(".csv"):
                 fp = os.path.join('tmp', file)
                 logmsg = ("File was not compressed ", fp)
                 instalog.appendMessage('INFO', logmsg)
                 with open(fp, 'rb') as f_in:
                     with open(newfile_path, 'wb') as f_out:
                         shutil.copyfileobj(f_in, f_out)
                         file_downloaded = True
    except IOError:
        time.sleep(2)
        pass

建议我现在这样做

for file in os.listdir("tmp"):
            file_downloaded = False
            while not file_downloaded:
                try:
                    if file.endswith("csv.gz"):
                        fp = os.path.join('tmp', file)
                        logmsg = ("Extracting ", fp)
                        instalog.appendMessage('INFO', logmsg)
                        with gzip.open(fp, 'r') as f_in:
                            with open(newfile_path, 'wb') as f_out:
                                shutil.copyfileobj(f_in, f_out)
                                file_downloaded = True
                    elif file.endswith(".csv"):
                        fp = os.path.join('tmp', file)
                        logmsg = ("File was not compressed ", fp)
                        instalog.appendMessage('INFO', logmsg)
                        with open(fp, 'rb') as f_in:
                          with open(newfile_path, 'wb') as f_out:
                                shutil.copyfileobj(f_in, f_out)
                                file_downloaded = True
                except IOError:
                    time.sleep(5)

【问题讨论】:

  • 我不确定我是否理解您的问题。您是在问如何将异常处理放入循环中?
  • 如果出现错误 errno,我想重新尝试下载 gz,但请等待几秒钟再重试!

标签: python try-catch except


【解决方案1】:
def unzip_gzip(source_path, destination_path):
    with gzip.open(source_path, "rb") as f_in:
        with open(destination_path, "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)


def process_file(file_name):
    if file_name.endswith("csv.gz"):
        full_path = os.path.join("tmp", file)
        instalog.appendMessage("INFO", ("Extracting ", full_path))
        unzip_gzip(full_path, new_file_path)

    elif file_name.endswith(".csv"):
        full_path = os.path.join("tmp", file)
        instalog.appendMessage("INFO", ("File was not compressed ", full_path))
        shutil.copyfile(full_path, new_file_path)

    else:
        return


for file in os.listdir("tmp"):
    success = False
    while not success:
        try:
            process_file(file)
            success = True
        except IOError:
            time.sleep(2)

【讨论】:

  • 我理解你的代码,我不想做一个函数,我尝试后如何回到顶部?
  • 我将像我尝试做的那样编辑和添加代码。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2018-03-20
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多