- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个 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/
我是初学者。在这里学习。 一段代码抛出 WindowsError。我捕捉到它并将其重命名为 exception。 我通过dir(exception)、type(exception)、print(exc
我已经创建了一个临时文件。 向创建的文件添加了一些数据。 保存了它,然后试图删除它。 但我遇到了 WindowsError。编辑后我关闭了文件。如何检查哪个其他进程正在访问该文件。 C:\Docume
我正在学习 Python(使用 Python 3.2)并且一直在遵循一些手册。其中之一,我被要求将当前目录更改为包含我被要求制作并放置在那里的特定文件的目录。我一直遇到以下错误: >>> import
Python 内置了通用 OSError 的异常。 , 它有 WindowsError它继承自 OSError。文档解释说 WindowsError 是 Raised when a Windows-s
使用Python的rename()函数重命名文件时出现问题,提示 WindowsError: [Error 2] 错误,最初代码如下: ?
这个简短的函数只是获取存储设备的可用空间,但是在运行代码时出现上述错误。 功能是: def disk_space1(drive): freespace = ctypes.c_ulonglong
这是我的问题的一个简化示例: import os import sqlite3 with sqlite3.connect('test.db.temp') as db: db.executesc
关于 Windows 上 Python 的快速问题。我有一个编译程序的脚本(使用安装规则),然后通过网络将构建产品移动到远程目标。 但是,我不断收到 WindowsError 5 Access Den
我正在尝试了解 Windows 8 上 Python 2.7.6 的异常。 这是我正在测试的代码,旨在在 My_New_Dir 中创建一个新目录。如果目录已经存在,我想删除整个目录及其内容,然后创建一
我在使用Scrapy的FifoDiskQueue时遇到了这个问题。在 Windows 中,FifoDiskQueue 将导致目录和文件由一个文件描述符创建并由另一个文件描述符使用(如果队列中没有更多消
我正在使用 killableprocess 包(建立在子进程之上)来运行进程每当我在脚本中运行“killableprocess.Popen(command)”这段代码时,我都会收到以下错误: File
我正在尝试在 QGIS 上开发 python 插件,并且我正在尝试使用子进程执行二进制程序: program = os.path.join(self.tranusConf.tranusBinPath,
setup.py from distutils.core import setup import py2exe setup(console=['program.py']) 错误 Traceback (
在我的程序中,我有一个函数 runScript(): def runScript(): subprocess.call(['echo', 'hello']) 我在 Python 文档中看到过许多类似的
在 Windows 上,当我尝试创建以下文件夹时: os.mkdir('H:\\__ Photos\\____Photos to be sorted\\[ Photo sorting proc
我正在尝试使用 Python 的 ctypes 来处理 DLL,但是当我尝试调用作为指向另一个函数的指针传递的函数时,我偶尔会遇到问题。 一点背景...我正在尝试使用 Dokan 构建用户空间文件系统
我正在尝试使用 Py2exe 构建我的独立可执行文件。我导入了 Pmw 类。当我运行 stanalone 可执行文件时,出现此错误: Traceback (most recent call last)
我在绘制 VBO 时遇到 PyOpenGL 函数 glDrawArrays() 问题。我有一个对象(类)ContactGeometry,我使用方法 create_VBO() 创建 VBO 并使用方法
我一直无法让 Python C API 给我错误。 背景: 我一直在使用 ctypes 运行 native 代码 (C++) 一段时间,但直到现在我还没有真正用 Python C 做过任何具体的事情应
运行 Windows 7 和 Python 2.7.8,使用 RotatingFileHandler 进行日志记录,并使用 subprocess.Popen 使得 RotatingFileHandle
我是一名优秀的程序员,十分优秀!