python - 如何将文件的创建日期附加到其文件名中?

标签 python

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

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

最佳答案

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)

关于python - 如何将文件的创建日期附加到其文件名中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158961/

相关文章:

python - 在python中以一定间隔从右边拆分一个字符串

Python 类型注解 : Proper way to annotate functions returning library object

python - Django fixture 数据没有有效的模型标识符?

python - python导入全局变量的两种方式

python - 在 Keras 模型中获取中间层输出的正确方法?

python - 使用 South (Mysql) 在 Django 中迁移问题

python - Altair LOESS 拟合值低于平均值,远低于线性回归

python - 将表达式解析为列表

python - 渲染时出现 TemplateSyntaxError/捕获 IOError : (13, 'Permission denied' )

python - 只保留特定领域的关键字?