Python多次复制文件夹
我正在写一段代码,让用户输入一个文件夹的路径,然后我想让代码询问用户想要复制的次数。接着,代码会开始复制这个文件夹,并把复制的文件夹按顺序命名,比如这样:
How many times you want to copy folder "moh"?
5
然后会创建5个文件夹的副本,分别命名为(1,2,3,4,5)
progs = int(raw_input( "Progs Number : "))
fullPath = currentDirectory + str(name)
if os.path.isdir( fullPath ):
# Directory name is legitimate and not already existent
shutil.rmtree ( fullPath )
os.mkdir( fullPath )
shutil.copy(moh, ) # This where the code should do the copying process but I don't know how to make the process repeated by the user input and rename the folder
else:
os.mkdir( fullPath )
1 个回答
2
当你想要做某件事情 N 次时,通常的做法是用一个循环来遍历 range(n)
:
for i in range(progs):
现在,i
是一个从 0 到 4 的数字,而你想要在 fullpath
中得到一个包含 i+1
字符串值的路径,对吧?那么,把这个意思翻译成 Python 代码:
pathname = os.path.join(fullpath, str(i+1))
现在,你知道该复制到哪里了:
shutil.copy(moh, pathname)