目录
前言:
探索:
读取文件的属性时间
更改文件属性时间
使用 win32file 修改时间属性
all code:
前言:
有时候需要修改文件的“修改时间” 、 “访问时间”,“创建时间” 使用 Python 写出来简单好用。
探索:
读取文件的属性时间
import os
import time
# 获取文件的基本属性
def get_data(file_path, change):
# 文件创建时间
create_time = os.path.getctime(file_path)
create_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))
# 文件的修改时间
modification_time = os.path.getmtime(file_path)
modification_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modification_time))
# 文件的访问时间
access_time = os.path.getatime(file_path)
access_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time))
table.add_row(create_time1, modification_time1, access_time1, change)
更改文件属性时间
import os
import time
def change_time(file_path):
now = time.time() # 获取时间戳
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) # 当前时间
os.utime(file_path, (now, now))
注意:这里无法修改创建时间,只能走另一种方法:
使用 win32file 修改时间属性
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
createTime = "2019-12-13 21:51:02" # 创建时间
modifyTime = "2019-02-02 00:01:03" # 修改时间
accessTime = "2019-02-02 00:01:04" # 访问时间
# 修改文件时间
def modifyFileTime(filePath ):
try:
format_str = "%Y-%m-%d %H:%M:%S" # 时间格式
f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0)
create_time = datetime.datetime.strptime(createTime, format_str)
update_time = datetime.datetime.strptime(modifyTime, format_str)
access_time = datetime.datetime.strptime(accessTime, format_str)
SetFileTime(f, create_time, update_time, access_time)
CloseHandle(f)
return True
except Exception as e:
print(e)
return False
all code:
import os
import time
import datetime
import win32timezone
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
createTime = "2019-12-13 21:51:02" # 创建时间
modifyTime = "2019-02-02 00:01:03" # 修改时间
accessTime = "2019-02-02 00:01:04" # 访问时间
# 修改文件时间
def modifyFileTime(filePath ):
try:
format_str = "%Y-%m-%d %H:%M:%S" # 时间格式
f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0)
create_time = datetime.datetime.strptime(createTime, format_str)
update_time = datetime.datetime.strptime(modifyTime, format_str)
access_time = datetime.datetime.strptime(accessTime, format_str)
SetFileTime(f, create_time, update_time, access_time)
CloseHandle(f)
return True
except Exception as e:
print(e)
return False
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)
modifyFileTime(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}!')