Python多次复制文件夹

时间:2014-10-18 00:12:45

标签: python copy directory rename shutil

我正在编写一个代码,要求用户输入复制文件夹的输入。 我希望代码从用户那里获得次数然后开始复制文件夹并将其重命名为类似的系列:

How many times you want to copy folder "moh"?
5

然后创建名为(1,2,3,4,5)

的文件夹的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 个答案:

答案 0 :(得分: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)
相关问题