修改文件时间属性的方法

Windows下:

1. bat文件?

2. Python

import os,sys,time
from stat  import *
filename= ' ChenRef.bib '
# 指定期望修改后的时间
TimeForChange =  ' 2017-01-10 07:51:21 '
# 转换时间格式为long型
ConverTime = time.mktime(time.strptime( TimeForChange, ' %Y-%m-%d %H:%M:%S ') )
# print(TimeForChange+' 转换后:'+str(ConverTime))

print( ' -------------修改前---------------- ')
# #创建时间
print( ' 创建时间   '+time.ctime(os.path.getctime(filename)))
# #最后修改时间
print( ' 修改时间   '+time.ctime(os.path.getmtime(filename)))
# #访问时间
print( ' 访问时间   '+time.ctime(os.path.getatime(filename)))

# 修改文件时间戳
times=(ConverTime,ConverTime)
# 进行修改
os.utime(filename, times)

print' -------------修改后---------------- ')
# 创建时间
print( ' 创建时间   '+time.ctime(os.path.getctime(filename)))
# 最后修改时间
print( ' 修改时间   '+time.ctime(os.path.getmtime(filename)))
# 访问时间
print( ' 访问时间   '+time.ctime(os.path.getatime(filename)))  


 

 改成函数的版本:

import os,sys,time,random
from stat  import *

def changeFileTime(fname, newtime):
    ConverTime = time.mktime(time.strptime( newtime, ' %Y-%m-%d %H:%M:%S '))
    times=(ConverTime,ConverTime)
    os.utime(fname, times)


#  get the files in current dir
files = os.listdir()
for f  in files:
     if os.path.isdir(f):
        files.remove(f)

#  generate random times
filenum = len(files)
newtimes = []
for i  in range(filenum):
    hour   =  ' {:0>2} '.format(random.randint(15,20))
    minute =  ' {:0>2} '.format(random.randint(0,59))  # http://blog.csdn.net/handsomekang/article/details/9183303
    second =  ' {:0>2} '.format(random.randint(0,59))
    newtimes.append( ' 2015-04-26  '+hour+ ' : '+minute+ ' : '+second)

for (file, newtime)  in zip(files, newtimes):
    changeFileTime(file, newtime)


转载于:https://www.cnblogs.com/likeatree/p/4280388.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值