【问题标题】:Try except pass function not continuing尝试除了通过功能不继续
【发布时间】:2015-04-14 15:28:11
【问题描述】:

所以我需要帮助传递我的异常并继续我的脚本的其余部分。简短的背景故事是我需要在 ftp 服务器上获取文件的前 400kb,它有我需要的 +CONTENTS 文件夹。我预计文件末尾会出现错误,因为我在文件完成之前终止了 ftp 连接。在我的测试中,我可以终止 ftp 连接,但它在 {print "7"} 之后挂起:

gzip:标准输入:文件意外结束 tar:存档中出现意外的 EOF tar:错误不可恢复:现在退出

因此,脚本不会继续执行下一个 try 语句。我的代码目前如下,非常感谢任何建议。

try:
    with open(filename, 'wb') as file_to_write:
        print '1'

        def callback(chunk):
            print '2'
            file_to_write.write(chunk)
            print '3'
            if os.path.isfile(filename) and os.path.getsize(filename) > 400000:
                print os.path.getsize(filename), "FILE SIZE"
                subprocess.Popen(['tar', '-xvf', filename, '+CONTENTS'], stdout=subprocess.PIPE)
                print "4"
                try:
                    if os.path.isfile('+CONTENTS'):
                        last_size = []
                        content_size = os.path.getsize('+CONTENTS')
                        cnt_sz = content_size
                        last_size.append(cnt_sz)
                        print "5", last_size
                        print "6", cnt_sz
                        if max(last_size) == content_size:
                            print "7"
                            ftp.quit()
                except EnvironmentError:
                    pass
            else:
                print 'continuing'
                # time.sleep(.1)

        ftp.retrbinary("RETR %s" % filename, callback, 4096)
except EOFError, e:
    print e
    pass

【问题讨论】:

  • file_to_write.close() 在 except 子句中毫无意义,因为您使用 with 语句打开了文件。以这种方式打开文件的目的是处理自动关闭它。
  • 谢谢 Steven,我会把它拿出来

标签: python python-2.7 exception ftp try-catch


【解决方案1】:

所以我让它像这样工作,ftp.quit 仍然导致我在这部分出错,所以我稍后会在脚本中退出。

try:
    with open(filename, 'wb') as file_to_write:
        print '1'
        last_size = 0

        def callback(chunk):
            global last_size
            if last_size is None:
                last_size = 0
            print '2'
            file_to_write.write(chunk)
            print '3'
            if os.path.isfile(filename) and os.path.getsize(filename) > 400000:
                print os.path.getsize(filename), "FILE SIZE"
                subprocess.Popen(['tar', '-xvf', filename, '+CONTENTS'], stdout=subprocess.PIPE)
                print "4"
                try:
                    if os.path.isfile('+CONTENTS'):
                        content_size = os.path.getsize('+CONTENTS')
                        print "5", last_size
                        print "6", content_size
                        if last_size == content_size:
                            print "7"
                            raise Exception
                        last_size = content_size
                except EnvironmentError:
                    pass
            else:
                print 'continuing'

        ftp.retrbinary("RETR %s" % filename, callback, 4096)
except:
    pass

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多