汇总制定目录下的CSV 文件内容至统一目录中

通过脚本收集到终端计算机上的各类软件安装情况,并每台计算机生成一个计算机命名的CSV 文件,现在需要通过一个脚本简单的实现将文件目录内的所有文件汇总到一个文件中,故而写了如下一个小程序。

-- coding: UTF-8 --

import os
import csv

遍历指定目录,显示目录下的所有文件名及路径

def eachFile(filepath):
pathDir = os.listdir(filepath)
for allDir in pathDir:
child = os.path.join('%s%s%s' % (filepath,'\', allDir))
try:
readFile(child)
except:
print(child)

读取文件内容并打印

def readFile(filename):
csv_File = csv.reader(open(filename,encoding='utf-8'))
for row in csv_File:
SoftwareName= row
HostName=filename.split('\')[-1].split('.')[0]
csv_write.writerow((HostName,SoftwareName))

if name == 'main':
filePath = r'\V12CNDCZ01DCSP2\tmp\SoftwareList'
out = open('d:\csv.csv', 'a', newline='',encoding='utf-8')
csv_write = csv.writer(out, dialect='excel')
eachFile(filePath)

猜你喜欢

转载自blog.51cto.com/unicom/2329362
今日推荐