1.导入模块
import os
import pandas as pd
import time
2.利用for循环遍历
# 利用绝对路径和join方法遍历循环
for info in os.listdir('H:/各个成绩表'):
domain = os.path.abspath('H:/各个成绩表')
info = os.path.join(domain, info)
3.用os和pandas库获取所需信息并整合
# 获得info路径下的文件名
file_name = os.path.basename(info)
f_d = pd.DataFrame([file_name])
# 获得info路径下的文件创建时间
file_ct_time = time.asctime(time.localtime(os.path.getctime(info)))
f_ctmie = pd.DataFrame([file_ct_time])
# 获得info路径下的文件最后修改时间
file_at_time = time.asctime(time.localtime(os.path.getmtime(info)))
f_atime = pd.DataFrame([file_at_time])
# 获得info路径下的文件的用户id
file_uid = os.stat(info)
file_uid = file_uid.st_uid
f_uid = pd.DataFrame([file_uid])
# 将几个DataFrame 进行合并(横向合并)
file_check = pd.concat([f_d, f_ctmie, f_atime, f_uid], axis=1)
# 设置mode='a',追尾堆叠式输出
file_check.to_csv('file_check3.csv', mode='a', header=None, index=None, encoding='utf-8-sig')
1.os.path.basename() 获取目标路径下的文件名
2.os.path.getctime() 获取目标路径下文件的创建时间(得到是以秒为单位的数值)
3.os.path.getmtime() 获取目标路径下文件的最后修改时间(得到是以秒为单位的数值)
3.time.asctime() 用time库中的方法将数值转换为可读的’精准时间,如‘Sun Jul 19 14:27:50 2020
4.time.localtime() 发现时间于实际相差8小时(东八区),用此方法设置成当地时间,如‘Sun Jul 19 22:27:50 2020‘
5.pd.connat() 将上述信息转换为DataFrame类型,再合并。encoding的值是因为文件名有汉字
4.输出结果
![](https://img-blog.csdnimg.cn/img_convert/b1a9bc8f5a074f5da47a7b710830b52e.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/e351e51ec72b88f767bb1ea19beafc11.png)
5.其他相关参考
可以使用st_atime来访问最近的访问,使用st_ctime来获取文件创建时间。
import os,time
def get_information(directory):
file_list = []
for i in os.listdir(directory):
a = os.stat(os.path.join(directory,i))
file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
return file_list
print get_information("/")
- 其他相关参考
import os
import time
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
def get_FileCreateTime(filePath):
# '''获取文件的创建时间'''
# filePath = unicode(filePath,'utf8')
t = os.path.getctime(filePath)
return TimeStampToTime(t)
def get_FileModifyTime(filePath):
# '''获取文件的修改时间'''
# filePath = unicode(filePath, 'utf8')
t = os.path.getmtime(filePath)
return TimeStampToTime(t)
def get_FileAccessTime(filePath):
# '''获取文件的访问时间'''
# filePath = unicode(filePath, 'utf8')
t = os.path.getatime(filePath)
return TimeStampToTime(t)
def get_FileSize(filePath):
# '''获取文件的大小,结果保留两位小数,单位为MB'''
# filePath = unicode(filePath,'utf8')
fsize = os.path.getsize(filePath)
fsize = fsize / float(1024 * 1024)
return round(fsize, 2)
if __name__ == '__main__':
path = r"D:\Desktop\实际图每隔30分钟\kevin_picture_alg_need_collect"
print(get_FileCreateTime(path))
print(get_FileModifyTime(path))
print(get_FileAccessTime(path))
print(get_FileSize(path))