Python实现批量修改文件时间属性

 更新时间:2023年11月09日 09:25:08   作者:恋恋西风  
我们有时候需要修改文件的“修改时间” 、 “访问时间”,“创建时间” ,此时如果使用Python批量实现应该会方便很多,下面小编就来为大家介绍一下具体实现方法吧

前言

有时候需要修改文件的“修改时间” 、 “访问时间”,“创建时间” 使用 Python 写出来简单好用。

探索

读取文件的属性时间

import os
import time
 
# 获取文件的基本属性
def get_data(file_path, change):
    # 文件创建时间
    create_time = os.path.getctime(file_path)
    create_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))
 
    # 文件的修改时间
    modification_time = os.path.getmtime(file_path)
    modification_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modification_time))
 
    # 文件的访问时间
    access_time = os.path.getatime(file_path)
    access_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time))
 
    table.add_row(create_time1, modification_time1, access_time1, change)

更改文件属性时间

import os
import time
 
def change_time(file_path):
    now = time.time()  # 获取时间戳
    localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now))  # 当前时间
 
    os.utime(file_path, (now, now))

注意:这里无法修改创建时间,只能走另一种方法:

使用 win32file 修改时间属性

from win32con import FILE_FLAG_BACKUP_SEMANTICS
from win32con import FILE_SHARE_WRITE
from win32file import CloseHandle
from win32file import CreateFile
from win32file import GENERIC_WRITE
from win32file import OPEN_EXISTING
from win32file import SetFileTime
 
createTime = "2019-12-13 21:51:02"  # 创建时间
modifyTime = "2019-02-02 00:01:03"  # 修改时间
accessTime = "2019-02-02 00:01:04"  # 访问时间
 
# 修改文件时间
def modifyFileTime(filePath ):
    try:
        format_str = "%Y-%m-%d %H:%M:%S"  # 时间格式
        f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
                       FILE_FLAG_BACKUP_SEMANTICS, 0)
 
        create_time = datetime.datetime.strptime(createTime, format_str)
        update_time = datetime.datetime.strptime(modifyTime, format_str)
        access_time = datetime.datetime.strptime(accessTime, format_str)
        SetFileTime(f, create_time, update_time, access_time)
        CloseHandle(f)
 
        return True
    except Exception as e:
        print(e)
        return False

完整代码

import os
import time
import datetime
import win32timezone
 
from win32con import FILE_FLAG_BACKUP_SEMANTICS
from win32con import FILE_SHARE_WRITE
from win32file import CloseHandle
from win32file import CreateFile
from win32file import GENERIC_WRITE
from win32file import OPEN_EXISTING
from win32file import SetFileTime
 
createTime = "2019-12-13 21:51:02"  # 创建时间
modifyTime = "2019-02-02 00:01:03"  # 修改时间
accessTime = "2019-02-02 00:01:04"  # 访问时间
 
# 修改文件时间
def modifyFileTime(filePath ):
    try:
        format_str = "%Y-%m-%d %H:%M:%S"  # 时间格式
        f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
                       FILE_FLAG_BACKUP_SEMANTICS, 0)
 
        create_time = datetime.datetime.strptime(createTime, format_str)
        update_time = datetime.datetime.strptime(modifyTime, format_str)
        access_time = datetime.datetime.strptime(accessTime, format_str)
        SetFileTime(f, create_time, update_time, access_time)
        CloseHandle(f)
 
        return True
    except Exception as e:
        print(e)
        return False
 
 
dircount=0
filecount=0
# i负责记录深度;
def deepDir(filepath,flag=0):
    global filecount
    global dircount
    filepath+="/"
    file_list = os.listdir(filepath)
    flag+=2
    # 负责存放目录名称
    dirls=[]
    for tempfile in file_list:
        if os.path.isdir(filepath+"/"+tempfile):
            dirls.append(filepath+"/"+tempfile)
        else:
            filecount+=1
            print('-'*flag,end='')
            print(tempfile)
            modifyFileTime(filepath+"/"+tempfile)
    for tempfile in dirls:
        dircount+=1
        deepDir(tempfile,flag)
 
if __name__=="__main__":
    # try:
    dir=input('please copy your dir and paste here (Be sure to copy directly):')
    deepDir(dir.replace('\\','/'))
 
    print(f'completed file nums is:{filecount} and dir num is {dircount}!')

到此这篇关于Python实现批量修改文件时间属性的文章就介绍到这了,更多相关Python修改文件时间属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python高级语法之闭包和装饰器详解

    python高级语法之闭包和装饰器详解

    这篇文章主要介绍了python高级语法之闭包和装饰器详解,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • Python安装selenium包详细过程

    Python安装selenium包详细过程

    在本篇文章里小编给大家整理了关于Python安装selenium包详细过程,需要的朋友们可以学习下。
    2019-07-07
  • python安装mysql的依赖包mysql-python操作

    python安装mysql的依赖包mysql-python操作

    这篇文章主要介绍了python安装mysql的依赖包mysql-python操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • python基于双向链表实现LFU算法

    python基于双向链表实现LFU算法

    这篇文章主要为大家详细介绍了python基于双向链表实现LFU算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • python ssh 执行shell命令的示例

    python ssh 执行shell命令的示例

    这篇文章主要介绍了python ssh 执行shell命令的示例,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-09-09
  • Matlab、Python为工具解析数据可视化之美

    Matlab、Python为工具解析数据可视化之美

    下面介绍一些数据可视化的作品(包含部分代码),主要是地学领域,可迁移至其他学科,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-11-11
  • python中强大的format函数实例详解

    python中强大的format函数实例详解

    python中format函数用于字符串的格式化,这篇文章主要介绍了python中强大的format函数,需要的朋友可以参考下
    2018-12-12
  • pytorch自定义loss损失函数

    pytorch自定义loss损失函数

    这篇文章主要介绍了pytorch自定义loss损失函数,自定义loss的方法有很多,本文要介绍的是把loss作为一个pytorch的模块,下面详细资料需要的小伙伴可以参考一下
    2022-02-02
  • Python实现矩阵转置的方法分析

    Python实现矩阵转置的方法分析

    这篇文章主要介绍了Python实现矩阵转置的方法,结合实例形式较为详细的分析了Python实现矩阵转置的相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • Pytorch框架实现mnist手写库识别(与tensorflow对比)

    Pytorch框架实现mnist手写库识别(与tensorflow对比)

    这篇文章主要介绍了Pytorch框架实现mnist手写库识别(与tensorflow对比),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论