【问题标题】:How to append a file's creation date to its filename?如何将文件的创建日期附加到其文件名?
【发布时间】:2010-01-29 00:05:01
【问题描述】:

我想创建一个 python 脚本 将文件创建日期附加到文件名的末尾,同时保留一批 pdf 文档的原始文件名 (Report)。

directory = T:\WISAARD_Web Portal Projects\PortalLogging\WebLogExpert
filenames = Report.pdf

【问题讨论】:

  • 你在做的过程中究竟遇到了什么问题?

标签: python


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2011-07-10
    • 2010-12-20
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多