ERRNO 22 Python无效参数为什么此路径导致错误处理脚本出错?

2024-06-15 06:22:23 发布

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

我有一个python脚本,可以将我的文件从linux机器同步到外部HDD。一切正常,但它卡在这条线上了

[*INFO]| 2020.11.22D12.21.06.646485| createPath: Directory doesn't exists. Creating /media/genericUser/5A48F86048F83BF7/2020.11.22/crtFolder/2020/udemy/react/code/section_12_adding_routing_someApp/addinRouting/src/hoc/Aux/

代码如下:

def createPath(src, root,dest, dV,file):
    #if the destination doesn't have / add it
    if(dest[-1] != "/"):
        log(0,"createPath: Adding / to " + dest,file)
        dest = dest + "/"
    #Create the path to be created
    dest = dest + root.split(src)[-1] + "/"
    if(not os.path.exists(dest)):
        if dV['VERBOSE']:
            log(0,"createPath: Directory doesn't exists. Creating " + dest,file)
        try:
            os.makedirs(dest, exist_ok=True)
            return dest
        except OSError as e:
            log(1,"createPath: Failed to create " + dest + " with error " + e,file)  
    else:
        return dest

完整错误输出:

Traceback (most recent call last):
  File "/media/genericUser/cf35aee0-faeb-40bb-adac-88595e8f71fe/genericUser_hdd/crtFolder/2020/github/syncHDD/syncHDD.py", line 173, in createPath
    os.makedirs(dest, exist_ok=True)
  File "/usr/lib/python3.8/os.py", line 223, in makedirs
    mkdir(name, mode)
OSError: [Errno 22] Invalid argument: '/media/genericUser/5A48F86048F83BF7/2020.11.22/crtFolder/2020/udemy/react/code/section_12_adding_routing_someApp/addinRouting/src/hoc/Aux/'
During handling of the above exception, another exception occurred:

更新:已经在评论中提到,并且很快意识到了这一点。“e”没有定义。不确定为什么无法创建该路径

更新:这是有效的os.makedirs('/media/genericUser/5A48F86048F83BF7/2020.11.22/crtFolder/2020/udemy/react/code/section_12_adding_routing_someApp/addinRouting/src/'),所以/hoc/Aux就是问题所在

问题:

  1. 为什么hoc/Aux会引起问题
  2. 为什么不在except OSError as e:中定义e

Tags: srcifosexistsmediafiledestudemy
1条回答
网友
1楼 · 发布于 2024-06-15 06:22:23

我设法解决了这个问题

e是一个类,因此此log(1,"createPath: Failed to create " + dest + " with error " + e,file)失败

正确答案是:

log(1,"createPath: Failed to create " + dest + " with error " + e.strerror,file)

这不是唯一的问题。事实证明,我的代码无法处理该故障,因此要修复该故障,我让函数在遇到异常时返回''。现在,shutil仍试图复制到'',但失败并输出错误消息。仍然不知道为什么/hoc/Aux会引起问题。它可以很好地复制到不同的设备上。只是这个外部硬盘有这个问题

相关问题 更多 >