Python - making file copies

I have a file that I want to copy to the directory several times. It can be 100, it can be 1000. This is a variable.

I came up with this:

import shutil count = 0 while (count < 100): shutil.copy2('/Users/bubble/Desktop/script.py', '/Users/bubble/Desktop/pics') count = count + 1 

It puts 1 copy of the file in the directory, but only 1 file. I assume that it does not automatically add 2,3,4,5, etc. At the end of the file, as if you were copying and pasting.

Any ideas how to do this?

Sincerely.

+6
source share
1 answer

Use str.format :

 import shutil for i in range(100): shutil.copy2('/Users/bubble/Desktop/script.py', '/Users/bubble/Desktop/pics/script{}.py'.format(i)) 

To make it even more useful, you can add the format specifier {:03d} (3-digit numbers, ie 001, 002, etc.), or {:04d} (4-digit numbers, that is 0001, 0002 etc.) according to their needs as suggested by @Roland Smith .

+7
source

Source: https://habr.com/ru/post/950420/


All Articles