0

I would like to create a python script that appends the file created date to the end of the filename while retaining the oringinal file name (Report) for a batch of pdf documents.

directory = T:\WISAARD_Web Portal Projects\PortalLogging\WebLogExpert
filenames = Report.pdf
1
  • 2
    And what exactly is the problem you encounter while doing it? Commented Jan 29, 2010 at 0:08

1 Answer 1

1
import os,time
root="/home"
path=os.path.join(root,"dir1")
os.chdir(path)
for files in os.listdir("."):
    if files.endswith(".pdf"):
        f,ext = os.path.splitext(files)        
        d=time.ctime(os.path.getmtime(files)).split() #here is just example. you can use strftime, strptime etc to format your date as desired
        filedate = d[-1]+"-"+d[-2]+"-"+d[-3]
        newname = f+filedate+ext
        try: 
            os.rename(files,newname)
        except Exception,e:
            print e
        else:
            print "ok: renamed %s to %s " %(files,newname)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.