經過腳本收集到終端計算機上的各種軟件安裝狀況,並每臺計算機生成一個計算機命名的CSV 文件,如今須要經過一個腳本簡單的實現將文件目錄內的全部文件彙總到一個文件中,故而寫了以下一個小程序。小程序
import os
import csvide
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)excel
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))utf-8
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)it