In the past I have read a log file using python with the following code, which has always worked fine:
JavaScript
x
9
1
with open(r"24T23.log") as f, open('logfile.csv', 'w') as f2:
2
writer = csv.writer(f2)
3
writer.writerow(['Index','Date', 'Time', 'Logic', '(Logic)','Type', 'Code','Connector', 'Message', 'Extra 1', 'Extra 2'])
4
5
i = 0
6
for line in f:
7
writer.writerow([i] + line.rstrip().split('t'))
8
i += 1
9
For a particular use case that I am working on, I need to read multiple files contained in a folder. Can someone please suggest how to modify the above code (I tried that using blob but could not succeed)? Thanks in advance.
Advertisement
Answer
How’s this?
JavaScript
1
23
23
1
import csv
2
import os
3
4
# From root of cwd
5
DIRECTORY = 'test'
6
7
# This gets the path of every file in DIRECTORY if it is a log file
8
# os.path.join(os.getcwd(),x) to include cwd in directory name
9
logs = [os.path.join(os.getcwd(), x) for x in os.listdir(
10
DIRECTORY) if os.path.splitext(x)[1] == '.log']
11
12
for log in logs:
13
# Note the 'a' append write, to append the rows to the files
14
with open(log) as f, open('logfile.csv', 'a') as f2:
15
writer = csv.writer(f2)
16
writer.writerow(['Index', 'Date', 'Time', 'Logic', '(Logic)',
17
'Type', 'Code', 'Connector', 'Message', 'Extra 1', 'Extra 2'])
18
19
i = 0
20
for line in f:
21
writer.writerow([i] + line.rstrip().split('t'))
22
i += 1
23