【发布时间】:2021-09-11 09:53:41
【问题描述】:
我的文件夹中有一个 .doc 文件。我想用不同的名称多次保存同一个 .doc 文件。
例子:
我有一个名为 myfile.doc 的 .doc 文件。
我希望将其保存为myfile1.doc、myfile2.doc、myfile3.doc。
最多需要对 30 个这样的文件执行此操作。
如何在 Python 中做到这一点?
我试过这个脚本。但是有一个错误:-
import shutil
dateoffile = input('Please Enter 4 Digit Date')
filenumber = input('Please Enter File Number')
hyphen1 = '-'
doctrname = 'KW'
hyphen2 = '-'
year = '2011'
original = 'dateoffile+hyphen1+doctrname+hyphen2+year.doc'
for i in range(1,31): # creates 30 files named from 1 to 30
new = filenumber+i+'.doc' # the +i+ is used to have 30 different names and not crash
shutil.copyfile(original, new) # You can even do original='pathtofile/orienter code hereginaldocfile.doc' and new='pathtonewfile/newdocfile'+i+'.doc'
错误是:-
new = filenumber+i+'.doc' # +i+ 用于拥有 30 个不同的名称并且不会崩溃 TypeError:只能将str(不是“int”)连接到str
你能解释一下为什么会这样吗???
【问题讨论】:
-
到目前为止,您尝试了哪些方法,遇到了哪些问题?你能把这些都贴出来吗?
标签: python python-3.x rename .doc