我做了一个list, 要依LIST中的字串变更资料夹中的word档名
ex:
folder中 N个docx
第一份word = test1.docx
List[0] = 测试
第一个word = test2.docx
List[1] = 测试2
要将folder 中第一份word 改名为 测试.docx, 第二份改为测试2.docx
continue...
code 如下:
import os
from docx import Document
##把命名的存在下列目录的docnamelist.txt档里
NewNameListLocation = r"E:\Python"
list = []
##读docnamelist里的内容并显示:
with open(r'E:\Python\docnamelist.txt', 'r', encoding = "utf-8") as
nametorenew:
for content in nametorenew:
content_line = content.replace('\n','')
## print(content_line)
## print(content)
list.append(content_line)
##单纯检查是否有写入list中
for items in list:
print(items)
##下段是做更名的动作
##把要更名的DOC存在此FOLDER中
Doctargetpath = r"E:\Python\renamefiles"
oldnames = os.listdir(Doctargetpath)
i=0
a=0
for a in range(len(oldnames)):
print(oldnames[a])
path_name = Doctargetpath + "\\" + oldnames[a]
try:
document = Document(path_name)
except:
print("Error!!")
##将第N个DOC档名更名为LIST中的第N个字串
else:
newname = Doctargetpath + str(items[i]) + '.docx'
##try:
os.rename(path_name, newname)
i=i+1
但~~~~
error msg:
Traceback (most recent call last):
File
"C:/Programs/Python/Python38-32/renametest.py",
line 33, in <module>
os.rename(path_name, newname)
FileExistsError: [WinError 183] 当档案已存在时,无法建立该档案。:
'E:\\Python\\renamefiles\\renamefilesF - 复制 (10) - 复制.docx' ->
'E:\\Python\\renamefilesF.docx'
请问...我那里做错了?? 可以指导一下吗?? 谢谢