避免程序因 I/O 错误而退出

发布于 2024-08-02 06:10:09 字数 1108 浏览 7 评论 0原文

我有一个广泛使用shutil.copy2 的Python 脚本。由于我使用它通过网络复制文件,因此经常出现 I/O 错误,从而导致程序执行中止:

Traceback (most recent call last):
  File "run_model.py", line 46, in <module>
    main()
  File "run_model.py", line 41, in main
    tracerconfigfile=OPT.tracerconfig)
  File "ModelRun.py", line 517, in run
    self.copy_data()
  File "ModelRun.py", line 604, in copy_ecmwf_data
    shutil.copy2(remotefilename, localfilename)
  File "/usr/lib64/python2.6/shutil.py", line 99, in copy2
    copyfile(src, dst)
  File "/usr/lib64/python2.6/shutil.py", line 54, in copyfile
    copyfileobj(fsrc, fdst)
  File "/usr/lib64/python2.6/shutil.py", line 27, in copyfileobj
    buf = fsrc.read(length)
IOError: [Errno 5] Input/output error

如何避免程序执行中止并让它重试复制过程?

我正在使用的代码已经通过检查文件大小来检查文件是否实际上已完全复制:

def check_file(file, size=0):
    if not os.path.exists(file):
        return False
    if (size != 0 and os.path.getsize(file) != size):
        return False
    return True

while (check_file(rempdg,self._ndays*130160640) is False):
    shutil.copy2(locpdg, rempdg)

I have a Python script using shutil.copy2 extensively. Since I use it to copy files over the network, I get too frequent I/O errors, which lead to the abortion of my program's execution:

Traceback (most recent call last):
  File "run_model.py", line 46, in <module>
    main()
  File "run_model.py", line 41, in main
    tracerconfigfile=OPT.tracerconfig)
  File "ModelRun.py", line 517, in run
    self.copy_data()
  File "ModelRun.py", line 604, in copy_ecmwf_data
    shutil.copy2(remotefilename, localfilename)
  File "/usr/lib64/python2.6/shutil.py", line 99, in copy2
    copyfile(src, dst)
  File "/usr/lib64/python2.6/shutil.py", line 54, in copyfile
    copyfileobj(fsrc, fdst)
  File "/usr/lib64/python2.6/shutil.py", line 27, in copyfileobj
    buf = fsrc.read(length)
IOError: [Errno 5] Input/output error

How can I avoid abortion of my program's execution and have it retry the copy process instead?

The code I'm using already checks that the file is actually copied completely by checking the filesize:

def check_file(file, size=0):
    if not os.path.exists(file):
        return False
    if (size != 0 and os.path.getsize(file) != size):
        return False
    return True

while (check_file(rempdg,self._ndays*130160640) is False):
    shutil.copy2(locpdg, rempdg)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世俗缘 2024-08-09 06:10:09

哪个块给出了错误?只需在其周围包裹一个 try/ except 即可:

def check_file(file, size=0):
    try:
        if not os.path.exists(file):
            return False
        if (size != 0 and os.path.getsize(file) != size):
            return False
        return True
    except IOError:
        return False # or True, whatever your default is

while (check_file(rempdg,self._ndays*130160640) is False):
    try:
        shutil.copy2(locpdg, rempdg)
    except IOError:
        pass # ignore the IOError and keep going

Which block is giving the error? Just wrap a try/except around it:

def check_file(file, size=0):
    try:
        if not os.path.exists(file):
            return False
        if (size != 0 and os.path.getsize(file) != size):
            return False
        return True
    except IOError:
        return False # or True, whatever your default is

while (check_file(rempdg,self._ndays*130160640) is False):
    try:
        shutil.copy2(locpdg, rempdg)
    except IOError:
        pass # ignore the IOError and keep going
ゃ懵逼小萝莉 2024-08-09 06:10:09

你可以用它

try:
    ...
except IOError as err:
    ...

来捕获错误并处理它们

看看这个

you can use

try:
    ...
except IOError as err:
    ...

to catch the errors and treat them

Have a look on this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文