python如何修改文件时间属性


Posted in Python onFebruary 05, 2021

1、获取文件的创建、修改、访问时间

# -*- encoding=utf-8 -*-
import os
import time


def get_file_time(filename):
  filename = os.path.abspath(filename)
  create_time = os.path.getctime(filename) # 创建时间
  print('old create time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))))
  update_time = os.path.getmtime(filename) # 修改时间
  print('old update time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_time))))
  access_time = os.path.getatime(filename) # 访问时间
  print('old access time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time))))
  return create_time, update_time, access_time


if __name__ == '__main__':
  get_file_time('E:/a.txt')

python如何修改文件时间属性

python如何修改文件时间属性

 2、更改文件的修改、访问时间(创建时间没查到怎么修改,暂时不记录)

# -*- encoding=utf-8 -*-
import os
import time

def set_file_time(filename, updatetime, access_time):
  # 先传修改时间,再传访问时间
  filename = os.path.abspath(filename)
  new_updatetime = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S'))
  new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S'))
  os.utime(filename, (new_access_time, new_updatetime))


if __name__ == '__main__':
  set_file_time('E:/a.txt', '2018-01-08 10:50:20', '2019-07-15 04:03:01')

python如何修改文件时间属性

 3、放在同一个py方便直接复制使用

# -*- encoding=utf-8 -*-
import os
import time


def get_file_time(filename):
  filename = os.path.abspath(filename)
  # 创建时间
  create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(filename)))
  # 修改时间
  update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename)))
  # 访问时间
  access_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getatime(filename)))
  return create_time, update_time, access_time


def set_file_time(filename, updatetime, access_time):
  # 先传修改时间,再传访问时间
  filename = os.path.abspath(filename)
  new_update_time = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S'))
  new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S'))
  os.utime(filename, (new_access_time, new_update_time))


def debug():
  create_time, update_time, access_time = get_file_time('E:/a.txt')
  set_file_time('E:/a.txt', update_time, access_time)
  get_file_time('E:/a.txt')


if __name__ == '__main__':

  debug()

 4、补充修改文件的创建时间

import os
import time

from pywintypes import Time # 可以忽视这个 Time 报错(运行程序还是没问题的)
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


def modify_file_create_time(filename, create_time_str, update_time_str, access_time_str):
  try:
    format_str = "%Y-%m-%d %H:%M:%S" # 时间格式
    # f = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
    f = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS, 0)
    create_time = Time(time.mktime(time.strptime(create_time_str, format_str)))
    update_time = Time(time.mktime(time.strptime(update_time_str, format_str)))
    access_time = Time(time.mktime(time.strptime(access_time_str, format_str)))
    SetFileTime(f, create_time, update_time, access_time)
    CloseHandle(f)
    print('update file time success:{}/{}/{}'.format(create_time_str, update_time_str,
                             access_time_str))
  except Exception as e:
    print('update file time fail:{}'.format(e))


if __name__ == '__main__':
  cTime = "2019-12-13 21:51:02" # 创建时间
  mTime = "2019-02-02 00:01:03" # 修改时间
  aTime = "2019-02-02 00:01:04" # 访问时间
  fName = r"a.txt" # 可以是文件也可以是文件夹
  print(os.path.isdir(fName))
  modify_file_create_time(fName, cTime, mTime, aTime)

以上就是python如何修改文件时间属性的详细内容,更多关于python修改文件时间属性的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python可跨平台实现获取按键的方法
Mar 05 Python
讲解Python中for循环下的索引变量的作用域
Apr 15 Python
给Python入门者的一些编程建议
Jun 15 Python
python清除指定目录内所有文件中script的方法
Jun 30 Python
一波神奇的Python语句、函数与方法的使用技巧总结
Dec 08 Python
Python中模块string.py详解
Mar 12 Python
python实现二维插值的三维显示
Dec 17 Python
python集合是否可变总结
Jun 20 Python
Tornado实现多进程/多线程的HTTP服务详解
Jul 25 Python
Django模型修改及数据迁移实现解析
Aug 01 Python
Kears+Opencv实现简单人脸识别
Aug 28 Python
手残删除python之后的补救方法
Jun 26 Python
虚拟环境及venv和virtualenv的区别说明
Feb 05 #Python
Pycharm 如何一键加引号的方法步骤
Feb 05 #Python
Python tkinter之Bind(绑定事件)的使用示例
Feb 05 #Python
pycharm配置python 设置pip安装源为豆瓣源
Feb 05 #Python
在PyCharm中安装PaddlePaddle的方法
Feb 05 #Python
python实现录制全屏和选择区域录屏功能
Feb 05 #Python
pycharm 使用anaconda为默认环境的操作
Feb 05 #Python
You might like
如何在PHP中使用Oracle数据库(3)
2006/10/09 PHP
php二维数组用键名分组相加实例函数
2013/11/06 PHP
php递归方法实现无限分类实例代码
2014/02/28 PHP
PHP微信红包API接口
2015/12/05 PHP
硬盘浏览程序,保存成网页格式便可使用
2006/12/03 Javascript
调试Node.JS的辅助工具(NodeWatcher)
2012/01/04 Javascript
图标线性回归斜着移动到指定的位置
2013/08/16 Javascript
jquery新的绑定事件机制on方法的使用方法
2014/04/15 Javascript
深入理解JavaScript编程中的原型概念
2015/06/25 Javascript
JS特效实现图片自动播放并可控的效果
2015/07/31 Javascript
javascript面向对象程序设计高级特性经典教程(值得收藏)
2016/05/19 Javascript
详解jQuery的表单验证插件--Validation
2016/12/21 Javascript
Vue.js基础知识小结
2017/01/13 Javascript
react中使用swiper的具体方法
2018/05/15 Javascript
小程序和web画三角形实现解析
2019/09/02 Javascript
初学python数组的处理代码
2011/01/04 Python
python处理文本文件实现生成指定格式文件的方法
2014/07/31 Python
python 循环遍历字典元素的简单方法
2016/09/11 Python
Python查询IP地址归属完整代码
2017/06/21 Python
读取json格式为DataFrame(可转为.csv)的实例讲解
2018/06/05 Python
Python高级特性与几种函数的讲解
2019/03/08 Python
django xadmin中form_layout添加字段显示方式
2020/03/30 Python
keras中的backend.clip用法
2020/05/22 Python
结束运行python的方法
2020/06/16 Python
海信商城:海信电视、科龙空调、容声冰箱官方专卖
2017/02/07 全球购物
伦敦眼门票在线预订:London Eye
2018/05/31 全球购物
澳大利亚最便宜的网上药房:Chemist Warehouse
2020/01/30 全球购物
介绍一下SQL Server里面的索引视图
2016/07/31 面试题
全国道德模范事迹
2014/02/01 职场文书
家长会演讲稿
2014/04/26 职场文书
某某同志考察材料
2014/05/28 职场文书
社区党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
2014年创卫工作总结
2014/11/24 职场文书
2014年中班下学期工作总结
2014/12/11 职场文书
2016年六一文艺汇演开幕词
2016/03/04 职场文书
JVM的类加载器和双亲委派模式你了解吗
2022/03/13 Java/Android