UltraDebug

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: A C D R G Y M Z X S P
公益项目,接受捐赠
查看: 1950|回复: 0
收起左侧

[Python] 给文件添加时间、姓名等后缀

[复制链接]
GDDDDWWWW

主题

0

回帖

UD

新手上路

UID
48
积分
33
注册时间
2022-5-18
最后登录
1970-1-1
2023-2-21 21:05:47 | 显示全部楼层 |阅读模式
因为最近需要重复的多次提交修改后的文件,写了一个小工具,如下:

[Python] 纯文本查看 复制代码
import datetime
import os
 
 
def main(filename_pre, fileauthor, filetype_pre):
    # print(filetype_pre)
    path = r'./'  # 路径为当前文件夹
    filelist = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
    for files in filelist:  # 遍历所有文件
        Olddir = os.path.join(path, files)  # 还是原来的文件路径
        if os.path.isdir(Olddir):
            continue  # 如果是文件夹则跳过
        else:  # 反之,则继续
            # print(Olddir)
            filenamepre = str(Olddir)  # 转化为字符串
            filenamepre = filenamepre.replace("[", "").replace("]", "").replace("'", "").replace(",", "\n").replace(" ","").replace( "./", "")  # replace替换"["、"]"、" "、"'" "./"
            firstname = os.path.splitext(filenamepre)[-2]  # 文件名
            lastname = os.path.splitext(filenamepre)[-1]  # 文件后缀
            if filename_pre == '':
                filename = str(str(firstname).split('_')[0])  # 依据“_”对文件名拆分获取文件名
            else:
                filename = filename_pre
            # print(str(filename))
            # print(firstname)
            # print(lastname)
            a = datetime.date.today().year
            b = datetime.date.today().month
            c = datetime.date.today().day
            d = datetime.datetime.now().hour
            e = datetime.datetime.now().minute
            # 获取时间
            if b < 10:
                b = '0' + str(b)
            if c < 10:
                c = '0' + str(c)
            if d < 10:
                d = '0' + str(d)
            if e < 10:
                e = '0' + str(e)
            # 如果不为10则前面加0
            filetime = str(a) + str(b) + str(c) + str(d) + str(e)
 
            if fileauthor == '':  # 作者名
                authorname = 'admin' # 默认为admin
            else:
                authorname = fileauthor # 填写的姓名后缀
            if filetype_pre == '':
                filetype_pre = '.doc'  # 默认文件扩展名为docx
            else:
                filetype_pre = filetype_pre  # 自定义目标文件扩展名
            # print(filetype_pre)
            if lastname == filetype_pre:  # 判断文件扩展名
                filetype = filetype_pre
                Newdir = os.path.join(path, filename + "_" + filetime + "_" + authorname + filetype)  # 新的文件路径
                os.rename(Olddir, Newdir)  # 完成重命名
 
 
if __name__ == '__main__':
    print('-----------------|***提示***|----------------')
    print('-----------------|输入文件名称|----------------')
    print('-----------------|例如:xxxxx|----------------')
    filename_pre = input('请输入文件名称,不填则默认为原文件名:') # 如需更改文件名则填写
    print('-----------------|***提示***|----------------')
    print('-----------------|输入作者姓名|----------------')
    print('-----------------|例如:admin|----------------')
    fileauthor = input('请输入作者名称,不填则默认为"admin":') # 姓名后缀
    print('-----------------|***提****示***|----------------')
    print('-----------------|输入格式:.xxxx|----------------')
    print('-----------------|例   如:.doc|----------------')
    filetype_pre = input('请输入文件后缀,不填则默认为".doc":')  # 自定义文件后缀名
 
    main(filename_pre, fileauthor, filetype_pre)
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
UltraDebug免责声明
✅以上内容均来自网友转发或原创,如存在侵权请发送到站方邮件9003554@qq.com处理。
✅The above content is forwarded or original by netizens. If there is infringement, please send the email to the destination 9003554@qq.com handle.
回复 打印

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|站点地图|UltraDebug ( 滇ICP备2022002049号-2 滇公网安备 53032102000034号)

GMT+8, 2025-6-20 14:23 , Processed in 0.031169 second(s), 10 queries , Redis On.

Powered by Discuz X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表