python - WindowsError : [Error 2] The system cannot find the file specified, 无法在 Python 中解析

标签 python python-2.7

我制作了一个 Python 程序,它可以清理我下载的 torrent 文件和文件夹中出现的不必要的名称,这样我就可以毫不费力地将它上传到我的无限 Google 云端硬盘存储帐户。

但是,它给了我:WindowsError: [Error 2] The system cannot find the file specified after certain number of iterations.如果我再次运行该程序,它可以正常工作一定的迭代,然后弹出相同的错误。

请注意,我已使用 os.path.join 采取预防措施来避免此错误,但它不断出现。由于这个错误,我必须在选定的文件夹/驱动器上运行该程序数十次。

这是我的程序:

import os
terms = ("-LOL[ettv]" #Other terms removed
)
#print terms[0]
p = "E:\TV Series"
for (path,dir,files) in os.walk(p):
    for name in terms:
        for i in files:
            if name in i:
                print i
                fn,_,sn = i.rpartition(name)
                os.rename(os.path.join(path, i), os.path.join(path, fn+sn))
        for i in dir:
            if name in i:
                print i
                fn,_,sn = i.rpartition(name)
                os.rename(os.path.join(path, i), os.path.join(path, fn+sn))

错误回溯:

Traceback (most recent call last):
File "E:\abcd.py", line 22, in <module>
os.rename(os.path.join(path, i), os.path.join(path, fn+sn))
WindowsError: [Error 2] The system cannot find the file specified

最佳答案

可能是由于 os.walk 的工作方式导致的子目录问题,即 path 在第一次子目录之后的下一次迭代。 os.walk 收集子目录的名称以在当前目录中的第一次迭代中访问进一步的迭代...

例如,在第一次调用 os.walk 时,您会得到:

('.', ['dir1', 'dir2'], ['file1', 'file2'])

现在您重命名文件(这可以正常工作),并且您将:'dir1' 重命名为 'dirA''dir2''dirB'

os.walk 的下一次迭代中,您将获得:

('dir1', ['subdir1-2', 'subdir1-2'], ['file1-1', 'file1-2'])

这里发生的是不再有 'dir1',因为它已经在文件系统上重命名了,但是 os.walk 仍然记得它在文件系统中的旧名称在里面列出并给你。现在,当您尝试重命名 'file1-1' 时,您要求输入 'dir1/file1-1',但在文件系统上它实际上是 'dirA/file1-1' 并且您收到错误。

要解决此问题,您需要更改 os.walk 在进一步迭代中使用的列表中的值,例如在你的代码中:

for (path, dir, files) in os.walk(p):
    for name in terms:
        for i in files:
            if name in i:
                print i
                fn, _, sn = i.rpartition(name)
                os.rename(os.path.join(path, i), os.path.join(path, fn+sn))
        for i in dir:
            if name in i:
                print i
                fn, _, sn = i.rpartition(name)
                os.rename(os.path.join(path, i), os.path.join(path, fn+sn))
                #here remove the old name and put a new name in the list
                #this will break the order of subdirs, but it doesn't
                #break the general algorithm, though if you need to keep
                #the order use '.index()' and '.insert()'.
                dirs.remove(i)
                dirs.append(fn+sn)

这应该可以解决问题,在上述情况下,将导致...

第一次调用 os.walk 时:

('.', ['dir1', 'dir2'], ['file1', 'file2'])

现在重命名:'dir1''dirA''dir2''dirB' 和如上所示更改列表...现在,在 os.walk 的下一次迭代中,它应该是:

('dirA', ['subdir1-2', 'subdir1-2'], ['file1-1', 'file1-2'])

关于python - WindowsError : [Error 2] The system cannot find the file specified, 无法在 Python 中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35681113/

相关文章:

python - 为什么对象类不允许属性?

python - 在一般情况下捕获错误参数异常

python - 如果函数在匹配来自不同列表的两个用户 ID 时执行函数?

python - 几个 .pyx 的 Cython setup.py

python - 正则表达式、Python 和文档注释 &lt;!-- 文本 -->

python - 当我尝试使用 subprocess.call 时,导致 "AttributeError: ' tuple' object has no attribute 'rfind' 的原因是什么

python-2.7 - 通过opencv检测 parking 场

python - Python 中的时间比较

python - Matlab 的交换误差

python - 如何在 Flask 中引用多个模型?