获取文件夹下的所有文件名,文件大小,更新时间
import os
import datetime
def get_files_info(folder_path):
files_info = []
# 获取起始路径
base_path = os.path.abspath(folder_path)
# 遍历文件夹下的所有文件和子文件夹
for root, dirs, files in os.walk(folder_path):
# 排除以英文点开头的子文件夹
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file_name in files:
file_path = os.path.join(root, file_name)
# 获取文件的相对路径
relative_path = os.path.relpath(file_path, base_path)
file_size = os.path.getsize(file_path)
file_update_time = os.path.getmtime(file_path)
file_update_time = datetime.datetime.fromtimestamp(file_update_time)
file_update_time = file_update_time.strftime('%Y-%m-%d %H:%M:%S')
file_info = {
'file_name': relative_path,
'file_size': file_size,
'file_update_time': file_update_time
}
files_info.append(file_info)
return files_info
标签:files,Python,update,文件夹,file,time,path,随笔,os
From: https://www.cnblogs.com/wanghong1994/p/17972055