How to save a .doc file many times with different names in the same folder

353 Views Asked by At

I have a .doc file in my folder. I want to save the same .doc file many times with different names.

Example:

I have a .doc file named myfile.doc.

I want it to be saved as myfile1.doc, myfile2.doc, myfile3.doc.

This needs to be done for up to 30 files like that.

How can I do this in Python?

I Tried this Script. But There is an Error:-

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'

Ther Error is :-

new = filenumber+i+'.doc' # the +i+ is used to have 30 different names and not crash TypeError: can only concatenate str (not "int") to str

Can You Explain Why it is Coming Like This ???

1

There are 1 best solutions below

5
Sozy On

you can try something like this :

import shutil

original = 'myfile.doc'
number=int(input("Enter a number to name your files : "))
for i in range(1,31): # creates 30 files named from 1 to 30
    num = number+i
    new = 'myfile'+str(num)+'.doc' # the +num+ is used to have 30 different names and not crash
    shutil.copyfile(original, new) # You can even do original='pathtofile/originaldocfile.doc' and new='pathtonewfile/newdocfile'+str(num)+'.doc'

This should work