查找目录及子目录所在文件的文件名存到execl表中

file_path=r"\\192.168.1.153\文件中心模块\受控文件模块\有效图纸模块\PDF模块\组件\整机图"
import os
import pandas as pds

def check_file(file_path):
all_file = os.listdir(os.chdir(file_path))
files = []
for f in all_file:
if os.path.isdir(f):
files.extend(check_file(file_path+'\\'+f))
os.chdir(file_path)
else:
files.append(f)
return files

file_list = check_file(file_path)
#print(file_list)
#print(len(file_list))
data_files = pds.DataFrame(file_list)
data_files.to_excel("E:\精益工艺部\FileNames.xls")
#print(dataframe)

-----------------------------------------------------------改版,指定日期后的文件名
import os
import time
import datetime
import pandas as pd

def get_SpecTime(befordate):
# 当前日期
today = datetime.datetime.now()
# 计算偏移量
offset = datetime.timedelta(days=befordate)
# 获取想要的日期
get_date = (today - offset).strftime('%Y-%m-%d')
return get_date

def getListFiles(path):
ret = []
for root, dirs, files in os.walk(path):
for filespath in files:
ret.append(os.path.join(root,filespath))
return ret

def getFilesDict(path,beforset):
result = getListFiles(path)
d_file = {}
for file in result:
file_ct = os.path.getatime(file)
file_offiec = time.strftime('%Y-%m-%d', time.localtime(file_ct))
if file_offiec > get_SpecTime(beforset):
file_name = os.path.basename(file)
d_file[file_name] = file_offiec
return d_file


def execlFiles(d_file):
# 提取KV值
keys = list(d_file.keys())
values = list(d_file.values())

result_excel = pd.DataFrame()
result_excel["文件名称"] = keys
result_excel["创建日期"] = values
# 写入excel
result_excel.to_excel("E:\精益工艺部\FileNames5.xls")


if __name__ == '__main__':
file_path = r'\\192.168.0.\**\电控资料'
d_file = getFilesDict(file_path,10)
print(d_file)
execlFiles(d_file)
posted @ 2021-05-27 19:26  冬日的温暖  阅读(140)  评论(0编辑  收藏  举报