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中使用strip()方法删除字符串中空格的教程
May 20 Python
python函数形参用法实例分析
Aug 04 Python
Python二叉树的定义及常用遍历算法分析
Nov 24 Python
pandas中去除指定字符的实例
May 18 Python
Python单元测试实例详解
May 25 Python
python组合无重复三位数的实例
Nov 13 Python
Python编写合并字典并实现敏感目录的小脚本
Feb 26 Python
详解python selenium 爬取网易云音乐歌单名
Mar 28 Python
Python语法之精妙的十个知识点(装B语法)
Jan 18 Python
使用Pycharm在运行过程中,查看每个变量的操作(show variables)
Jun 08 Python
python七种方法判断字符串是否包含子串
Aug 18 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实现的随机广告显示代码
2007/06/14 PHP
简单的php 验证图片生成函数
2009/05/21 PHP
linux下为php添加curl扩展的方法
2011/07/29 PHP
php利用smtp类实现电子邮件发送
2015/10/30 PHP
jQuery的链式调用浅析
2010/12/03 Javascript
js实现拖拽 闭包函数详细介绍
2012/11/25 Javascript
js判断客户端是iOS还是Android等移动终端的方法
2013/12/11 Javascript
javascript中Math.random()使用详解
2015/04/15 Javascript
浅谈DOCTYPE对$(window).height()取值的影响
2016/07/21 Javascript
微信小程序之仿微信漂流瓶实例
2016/12/09 Javascript
JavaScript trim 实现去除字符串首尾指定字符的简单方法
2016/12/27 Javascript
javascript 中关于array的常用方法详解
2017/05/05 Javascript
[07:27]DOTA2卡尔工作室 英雄介绍水晶室女篇
2013/06/21 DOTA
用smtplib和email封装python发送邮件模块类分享
2014/02/17 Python
python统计字符串中指定字符出现次数的方法
2015/04/04 Python
浅谈用VSCode写python的正确姿势
2017/12/16 Python
Python并发之多进程的方法实例代码
2018/08/15 Python
python pygame模块编写飞机大战
2018/11/20 Python
对Python的多进程锁的使用方法详解
2019/02/18 Python
python实现QQ批量登录功能
2019/06/19 Python
解决tensorflow打印tensor有省略号的问题
2020/02/04 Python
基于keras中的回调函数用法说明
2020/06/17 Python
浅析Python 抽象工厂模式的优缺点
2020/07/13 Python
Django缓存Cache使用详解
2020/11/30 Python
美国环保婴儿用品公司:The Honest Company
2017/11/23 全球购物
Converse匡威法国官网:美国著名帆布鞋品牌
2018/12/05 全球购物
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
美国排名第一的泳池用品直接来源:In The Swim
2019/09/23 全球购物
给校长的建议书400字
2014/05/15 职场文书
优秀三好学生事迹材料
2014/08/31 职场文书
土地转让协议书
2014/09/27 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
公安个人四风问题对照检查及整改措施
2014/10/28 职场文书
2015年检验科工作总结
2015/04/27 职场文书
硕士学位申请报告
2015/05/15 职场文书
2015年国培研修感言
2015/08/01 职场文书