【问题标题】:WindowsError: [Error 2] The system cannot find the file specifiedWindowsError: [错误 2] 系统找不到指定的文件
【发布时间】:2011-07-16 11:55:54
【问题描述】:

我对这段代码有疑问。我正在尝试重命名文件夹中的所有文件名,以便它们不再有 +'s !这已经工作了很多次,但突然我得到了错误:

WindowsError: [Error 2] The system cannot find the file specified at line 26

第 26 行是代码的最后一行。

有人知道为什么会这样吗?我只是向某人保证我可以在 5 分钟内完成这项工作,因为我有一个代码!可惜没用!!

import os, glob, sys
folder = "C:\\Documents and Settings\\DuffA\\Bureaublad\\Johan\\10G304655_1"

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        filename = os.path.join(root, filename)
old = "+"
new = "_"
for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        if old in filename:
            print (filename)
            os.rename(filename, filename.replace(old,new))

【问题讨论】:

  • 只是一个小评论。在更新问题以添加新信息时,如果您将原始问题留在那里,它会帮助其他读者,只需添加新位,以便我们查看历史记录。

标签: python renaming


【解决方案1】:

您正在使用splitext 来确定要重命名的源文件名:

filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
filename_zero = filename_split[0]#
...
os.rename(filename_zero, filename_zero.replace('+','_'))

如果遇到带有扩展名的文件,显然尝试重命名不带扩展名的文件名会导致“找不到文件”错误。

【讨论】:

  • 我尝试再次运行它,并没有拆分文件,但我得到了与以前相同的错误 - 我编辑了我的问题以包含我使用的其他脚本!
【解决方案2】:

我怀疑您的子目录可能有问题。

如果您有一个包含文件“a”、“b”的目录和包含文件“sub+1”和“sub+2”的子目录“dir”,则调用os.walk() 将产生以下值:

(('.',), ('dir',), ('a', 'b'))
(('dir',), (,), ('sub+1', 'sub+2'))

当您处理第二个元组时,您将以'sub+1', 'sub_1' 作为参数调用rename(),而您想要的是'dir\sub+1', 'dir\sub_1'

要解决此问题,请将代码中的循环更改为:

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:           
        filename = os.path.join(root, filename)
        ... process file here

它会在你对它做任何事情之前将目录与文件名连接起来。

编辑:

我认为以上是正确的答案,但不是完全正确的原因。

假设您在目录中有一个文件“File+1”,os.walk() 将返回

("C:/Documents and Settings/DuffA/Bureaublad/Johan/10G304655_1/", (,), ("File+1",))

除非你在“10G304655_1”目录下,当你调用rename()时,当前目录下不会找到文件“File+1”,因为那不是与os.walk() 正在查找的目录相同。通过调用os.path.join() yuo 告诉rename 查找正确的目录。

编辑 2

所需的代码示例可能是:

import os

# Use a raw string, to reduce errors with \ characters.
folder = r"C:\Documents and Settings\DuffA\Bureaublad\Johan\10G304655_1"

old = '+'
new = '_'

for root, dirs, filenames in os.walk(folder):
 for filename in filenames:
    if old in filename: # If a '+' in the filename
      filename = os.path.join(root, filename) # Get the absolute path to the file.
      print (filename)
      os.rename(filename, filename.replace(old,new)) # Rename the file

【讨论】:

  • 你的意思是这样吗?import os, glob, sys folder = "C:\\Documents and Settings\\DuffA\\Bureaublad\\Johan\\10G304655_1" for root, dirs, filenames in os.walk(folder): for filenames in filenames: filename = os.path.join(root, filename) old = "+" new = "_" for root, dirs, filenames in os.walk(folder): for filename在文件名中:如果在文件名中是旧的: print (filename) os.rename(filename, filename.replace(old,new))
  • 我已将代码添加到我原来的问题中!如果这些真的是我犯的基本错误,我很抱歉!我必须在没有资源的情况下自己学习这个!!
猜你喜欢
  • 1970-01-01
  • 2014-04-12
  • 2016-08-17
  • 2018-10-21
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
相关资源
最近更新 更多