Search code examples
pythonfiledirectoryincrement

Is it possible to make multiple copies of a file using shutil.copy()?


If you have a file that you want to make a copy of a certain number of times and then save them within another directory how would you go about it. I have a file named menu.txt and want to make 5 copies of the file in another directory and also by adding a numerical value to the name of the file so they don't override ex: (menu1.txt, menu2.txt, …)

for i in range(10)
    shutil.copy("menu.txt", path)

Would I need to implement this by using .split() or what?


Solution

  • Use a different filename for each copy.

    for i in range(1, 11)
        shutil.copy("menu.txt", os.path.join(path, f"menu{i}.txt"))
    

    This will create menu1.txt through menu10.txt in the destination directory.