#!/usr/bin/python
-*- coding: UTF-8 -*-
#功能要求
#1.通过表头检索把表头字符串在需求字符串内的csv移动到指定文件夹内
#2.把移动出来到csv合并,表头只用第一csv的(该处表头有数行)
#3.我的代码。。完全不行啊
import pandas as pd
import os
import csv
import shutil
import glob
def search_file(path):
headers = ['Test9','Test4','Development13','Development3']
queue = []
queue.append(path);
while len(queue) > 0:
tmp = queue.pop(0)
if (os.path.isdir(tmp)):#if it is folder
for item in os.listdir(tmp):
queue.append(os.path.join(tmp, item))#add the path into queue
elif (os.path.isfile(tmp)):#if it is file
csv_reader = csv.reader(open(tmp))
for row in csv_reader:
header = row
if header in headers:
shutil.move('/Users/Downloads/Test')#move file
search_file('/Users/Downloads')#if can work
#file setting
cwd = os.getcwd()
read_path = 'Test'
save_path = cwd
save_name = 'Test1.csv'
os.chdir(read_path)
#Add the file into list
csv_name_list = os.listdir()
#get first file
df = pd.read_csv( csv_name_list[0])
df.to_csv( cwd + '\' + save_path + '\' + save_name , encoding="utf_8",index=False)
#Done
n = glob.glob(pathname='*.csv')
for i in range(n):
df = pd.read_csv( csv_name_list[i] )
df.to_csv(cwd + '\' + save_path + '\' + save_name ,encoding="utf_8",index=False, header=False, mode='a+')