有时候会有一种需求,需要将目录中文件的访问时间、修改时间修改为近几天。使用python语言恰好可以非常简单的实现我们的需求,将需要修改的目录复制进来即可修改,为了实现仿真性,我们可以将时间戳规定在一定的范围,不会轻易被发现。我会将打包好的exe文件上传到资源中,有需要的可以自己去下载。
如下图所示:
使用过程如下:
复制路径:
开始更改:
完整代码如下:
import os
import random
import time
#月份字典
month_dict = {
'Jau':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
#文件路經
# filepath = 'C:/Users/123/Desktop/mysk-learn2/CatAndDog Sort/'
def alertTime(filepath):
# 下面一部分代码可以自己扩展,为了获取文件的创建修改访问时间
# fName_pre = os.path.splitext(fName)[0]
# fName_pre=os.path.splitext(fName)[0]+os.path.splitext(fName)[1]or""
# #获取文件的访问时间、改变时间、修改时间
# atime = time.ctime(os.path.getatime(filepath))
# ctime = time.ctime(os.path.getctime(filepath))
# mtime = time.ctime(os.path.getmtime(filepath))
#
# print('访问时间:',atime)
# print('改变时间:',ctime)
# print('修改时间:',mtime)
#
# #指定时间格式
# format = "%Y%m%d_%H%M%S"
#
# #转换访问时间、修改时间的内容格式
# a_Y = atime[20:24]
# a_m = month_dict[atime[4:7]]
# a_d = atime[8:10]
# a_H = atime[11:13]
# a_M = atime[14:16]
# a_S = atime[17:19]
# Access_time = a_Y + a_m + a_d+ '_' + a_H + a_M + a_S
# print('Access_time:',Access_time)
#
# c_Y = mtime[20:24]
# c_m = month_dict[mtime[4:7]]
# c_d = mtime[8:10]
# c_H = mtime[11:13]
# c_M = mtime[14:16]
# c_S = mtime[17:19]
# Modified_time = c_Y + c_m + c_d+ '_' + c_H + c_M + c_S
# print('Modified_time:',Modified_time)
#
# #创建struct_time对象
# atime_t = time.mktime(time.strptime(Access_time, format))
# mtime_t = time.mktime(time.strptime(Access_time, format))
# print(atime_t)
# print(mtime_t)
viewtime=time.time()+random.randint(-7200,7200)
altertime=time.time()+random.randint(-84600*2,-7200)
#修改访问时间和修改时间
os.utime(filepath, (viewtime, altertime))
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)
alertTime(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}!')
# except:
# print("error!")
为了方便平时使用,可以通过pyinstaller进行打包
在打包时遇到了报错 Failed to execute script ‘xxx‘ due to unhandled exception:input():lost sys.stdin
是因为在打包的时候使用了参数 -w,即无控制台窗口模式,导致需要命令行输入时无法执行并报错。
解决办法,使用打包命令: pyinstaller -F xxx.py,或写成 pyinstaller -F -c xxx.py(-c参数使用控制台子系统执行,默认方式)。