【问题标题】:Python renaming triggers error; file is being used by another processPython重命名触发错误;文件正被另一个进程使用
【发布时间】:2014-09-17 02:43:56
【问题描述】:

我收到以下错误消息,但我无法弄清楚为什么重命名会触发此错误消息:

Traceback(最近一次调用最后一次): 文件“C:\Apps\UtilitiesByMarc\make hypertext list from directory aaa.py”,第 17 行,在 os.rename(全名,nufullname) WindowsError: [错误 32] 进程无法访问该文件,因为它正被另一个进程使用

任何见解将不胜感激。

这是我的代码:

import os
dir_path = r'E:\ddelete'
dir_list = os.listdir(dir_path)

html_file = r"E:\ddelete\AAA__ReadMeFirst___dirlist.html"
text_file = open(html_file, "w")
i = 10000
for item in sorted(dir_list):
    fullname = os.path.join(dir_path,item)
    ext = ext = os.path.splitext(fullname)[1]
    nufullname = os.path.join(dir_path, str(i) + ext)
    nufilename = str(i) + ext
    print fullname
    print nufullname
    os.rename(fullname, nufullname)
    temp_item = '''<a href="./''' + nufilename + '''">''' + item + '</a><br />' + '\r\n'
    text_file.write(temp_item)
    i += 1

text_file.close()

【问题讨论】:

    标签: windows python-2.7 rename


    【解决方案1】:

    我刚刚意识到我正在尝试重命名我正在写入文本的 html_file。 很抱歉浪费了你的时间。 马克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2011-04-13
      相关资源
      最近更新 更多