修改文件时间属性的方法
2015-02-08 20:01
351 查看
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)
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)
相关文章推荐
- asp.net 获得文件属性中的修改时间,获得系统文件属性的方法,最后一次写入时间
- “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法
- 隐藏修改文件时间和文件属性的ASP脚本
- Mac OS X使用技巧:显示、隐藏文件、修改文件时间属性
- Windows平台上修改某个文件的创建、访问、修改日期时间属性
- VC ++实现 对文件的 隐藏, 只读 ,创建时间 , 最后修改时间 ,最后访问时间 属性的修改
- “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法
- 编程经验:修改文件属性时间
- vim修改文件属性的方法
- 文件时间属性获取与修改
- 设置文件属性里的时间,包括:创建时间、访问时间、修改时间
- linux批量修改文件时间的两种方法
- linux touch命令: 修改文件时间属性
- Linux 下修改文件的访问时间和修改时间属性
- 修改文件的时间属性(VS2005)
- Linux 下修改文件的时间属性
- 详解win7中的文件属性“访问时间”和“修改时间”不一致的原因分析 【原来LastAccessTime早就没用了】
- 为什么文件属性中的“访问时间”和“修改时间”不一致?
- Linux下修改文件的时间属性
- [原]VC实现修改文件创建、访问、修改时间属性