python\小脚本\循环读取目录文件.py
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 23 19:09:58 2018
@author: zhoujunqing
"""
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 27 14:56:03 2018
@author: zhoujunqing
"""
import os,re
import pandas as pd
import time
import json
import datetime
import tarfile
dir_path = r'C:\Users\zhoujunqing\Desktop\log_log'
col = ['time','op','userId'] ##选取需要的字段,看了下不是所有的字段都需要的
df = pd.DataFrame([],columns=col)
for root,dirs,files in os.walk(dir_path):##文件夹的路径
if files: ##判断是否有文件
for file_name in files: ##循环文件的名称
if 'o2o_store_order_data' in file_name: #判定是不是文件是否有o2o_order结尾的文件,是的就继续,不是的就退出了;
path = os.path.join(root,file_name)
print('正在处理的压缩文件是%s'%(path))
try:
data = pd.read_table(path,sep=',') ##路径
df_tmp = pd.DataFrame(data,columns= col,index=[0])
df = pd.concat([df,df_tmp]) ## 数据合并
except:
print('读取文件,处理数据失败')
else:
print('warning:文件不含o2o_store_order_data结尾的文件,不操作')
#
print(df.head())
#保存到本地
out_path= r'C:\\Users\\zhoujunqing\\Desktop\\output-1.xlsx'
writer = pd.ExcelWriter(out_path, engine='xlsxwriter')
df.to_excel(writer,'Sheet1')
writer.save()