重命名 txt 文件。编辑版本:[错误 183] 文件已存在时无法创建文件
Rename txt file. Edited version: [Error 183] Cannot create a file when that file already exists
我正在使用 python 2.7x。我是 python 的新手,非常感谢您的帮助。我已经阅读了很多帖子,包括下面显示的一些关于此错误的帖子:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat' --> 我的文件关闭指令已经结束了。
python 2 [Error 32] The process cannot access the file because it is being used by another process --> 我无法使用 shutil,因为我正在使用的 python 程序中存在一些错误,而且我无法编辑路径,因为我的计算机受管理保护。
Rename Files in Python --> 遵循建议后,我得到了 NameError: name 'rename' is not defined instead... :/
等等等等
在尝试纠正我的代码后,我仍然遇到同样的错误。
我想做的是:我读取目录中的文件,如果任何文件包含特定的字符串,我会重命名文本文件
(即 first.txt 到 firstfound.txt。)
编辑版本:
在重命名文件之前,我尝试移动 abc.close():
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
oldfile = fname
abc.close()
if oldfile == fname:
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
newfile = oldtitle +"found.txt"
os.rename(oldfile,newfile)
但是最后一行出现了这个错误。
os.rename(旧文件,新文件)
WindowsError:[错误 183] 文件已存在时无法创建该文件。我的文件夹中没有新名称的文件。
非常感谢您的建议!
已编辑版本 2:我也尝试过这组代码,但它给了我 WindowsError: [Error 5] Access is denied。请问有没有因为没有管理员权限而无法重命名txt文件的情况?谢谢!
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
oldfile = fname
abc.close()
if (oldfile == fname): #+'/' + "a.txt"
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
new_file = oldtitle +'/' + "a.txt"
os.rename(fname,new_file)
初始版本:我得到的错误是在 os.rename 行。 "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"
我的整个程序代码如下图:
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
old_file = fname
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
new_file = oldtitle +'/' + "found.txt"
os.rename(old_file,new_file) ##WindowsError: [Error 32] The process cannot access the file because it is being used by another process
abc.close()
我不明白为什么这个错误仍然存在。 (我已经关闭了所有文件和文件夹)。非常感谢你!
问题很可能是由于代码中的 open() 调用引起的。 python 中的 open() 函数打开 reading/writing 的文件文件,因此如果您打开一个文件,则无法对其调用重命名,因为它是在另一个位置打开的。
你应该调用
abc.close()
在重命名文件之前。
有关文件 I/O.
的更多信息,请参阅 this link
我正在使用 python 2.7x。我是 python 的新手,非常感谢您的帮助。我已经阅读了很多帖子,包括下面显示的一些关于此错误的帖子:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat' --> 我的文件关闭指令已经结束了。
python 2 [Error 32] The process cannot access the file because it is being used by another process --> 我无法使用 shutil,因为我正在使用的 python 程序中存在一些错误,而且我无法编辑路径,因为我的计算机受管理保护。
Rename Files in Python --> 遵循建议后,我得到了 NameError: name 'rename' is not defined instead... :/
等等等等
在尝试纠正我的代码后,我仍然遇到同样的错误。
我想做的是:我读取目录中的文件,如果任何文件包含特定的字符串,我会重命名文本文件 (即 first.txt 到 firstfound.txt。)
编辑版本: 在重命名文件之前,我尝试移动 abc.close():
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
oldfile = fname
abc.close()
if oldfile == fname:
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
newfile = oldtitle +"found.txt"
os.rename(oldfile,newfile)
但是最后一行出现了这个错误。 os.rename(旧文件,新文件) WindowsError:[错误 183] 文件已存在时无法创建该文件。我的文件夹中没有新名称的文件。 非常感谢您的建议!
已编辑版本 2:我也尝试过这组代码,但它给了我 WindowsError: [Error 5] Access is denied。请问有没有因为没有管理员权限而无法重命名txt文件的情况?谢谢!
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
oldfile = fname
abc.close()
if (oldfile == fname): #+'/' + "a.txt"
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
new_file = oldtitle +'/' + "a.txt"
os.rename(fname,new_file)
初始版本:我得到的错误是在 os.rename 行。 "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"
我的整个程序代码如下图:
import os
fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
if not line.find("scramble") :
continue
old_file = fname
for title in fname:
endname = title.find('.txt')
oldtitle = title[:endname]
new_file = oldtitle +'/' + "found.txt"
os.rename(old_file,new_file) ##WindowsError: [Error 32] The process cannot access the file because it is being used by another process
abc.close()
我不明白为什么这个错误仍然存在。 (我已经关闭了所有文件和文件夹)。非常感谢你!
问题很可能是由于代码中的 open() 调用引起的。 python 中的 open() 函数打开 reading/writing 的文件文件,因此如果您打开一个文件,则无法对其调用重命名,因为它是在另一个位置打开的。
你应该调用
abc.close()
在重命名文件之前。
有关文件 I/O.
的更多信息,请参阅 this link