Python copy not move

HI: I have a process where we email our invoices as pdf's to customers. I then extract the pdfs,  rename, and move to another directory using the python script below. The original files begin with the customer number; the renamed files begin with the date - so they can be sorted differently in explorer. However, the python script MOVES the file so I have been losing the originals and of course I sometimes need to sort invoices by customer (the first three characters of the original pdf). Does any python expert know how to change the script below from a MOVE process to a COPY?

target = ('C:\pythonrename\\files')
destination = ('C:\pythonrename\\invoices\\')
allfiles = os.listdir(target)
for filename in allfiles:
    t = os.path.getmtime(filename)
    v = datetime.datetime.fromtimestamp(t)
    x = v.strftime('%Y%m%d')
    p = filename
    py = p[0:3]
    newstuff = (destination + x + "_" + py +".pdf")
    if not  os.path.isfile(newstuff):
                  os.rename(filename, newstuff)
Richard TeasdaleFinancial ControllerAsked:
Who is Participating?
NorieVBA ExpertCommented:
Try using copy from shutil.
shutil.copy(filename, newstuff)

Open in new window

Richard TeasdaleFinancial ControllerAuthor Commented:
Thanks, Norie. You are a star.
Have a look also at shutil.copy2 that tries to preserve metadata (like last modification time -- if it matters).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.