如何避免获取ftplib.error\u perm:550由于另一个进程正在使用该文件,因此进程无法访问该文件?

2024-03-29 07:21:58 发布

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

我正在为我的公司编写一个python代码,用ftplib将许多图像文件上传到FTP服务器,但我的代码必须应用于网络不稳定。因此,当图像文件上载到FTP服务器时,可能会失败,然后我会得到一个错误“ftplib.error\u perm:550该进程无法访问该文件,因为它正被另一个进程使用。”。顺便说一下,FTP服务器由另一家公司管理

我的上传图像文件代码:

with open(img_list[0][0], "rb") as oFile:
    check_chars = oFile.read()[-2:]
# End of with-block
if check_chars != b"\xff\xd9":
    time.sleep(1)
    continue
# End of if-condition

print("準備上傳: %s" %(img_list[0][1]))
if cut_switch:
    img = Cut_image(img_list[0][0])
    ftp.storbinary('STOR '+img_list[0][1], img)
else:
    with open(img_list[0][0], 'rb') as imageReader:
        ftp.storbinary('STOR '+img_list[0][1], imageReader)
    # End of with-block
# End of if-else-condition

我希望有人能给我一些建议来解决这个错误


Tags: of代码服务器imgif进程错误图像文件
1条回答
网友
1楼 · 发布于 2024-03-29 07:21:58

试试这个:

    oFile = open(img_list[0][0], "rb") 
    check_chars = oFile.read()[-2:]
    if check_chars != b"\xff\xd9":
        time.sleep(1)
    oFile.close()

    print("準備上傳: %s" %(img_list[0][1]))
    if cut_switch:
        img = Cut_image(img_list[0][0])
        ftp.storbinary('STOR '+img_list[0][1], img)
    else:
        with open(img_list[0][0], 'rb') as imageReader:
            ftp.storbinary('STOR '+img_list[0][1], imageReader)

发生此错误是因为您尚未关闭文件(正在打开) 您应该记住,您不需要continue退出if-else条件

相关问题 更多 >