-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
56 lines (39 loc) · 1.47 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
import os
import csv
# functions used later
def DeleteBlanks(file):
"""Deletes blank lines at the end of a file"""
with open(file) as input_file:
data = input_file.read().rstrip('\n')
input_file.close()
with open(file, 'w') as output_file:
output_file.write(data)
output_file.close()
def ReadLastLine(file,number):
"""Returns a list of the last n lines of a given file"""
with open(file) as f:
data = []
for line in (f.readlines() [-number:]):
line = line.rstrip('\n')
data.append(line)
return(data)
# define where to look for files for parsing, type of files, and what to label the output columns
files_location = os.path.dirname(os.path.realpath(__file__))
file_extension = '.log'
dict_labels = ['Time', 'Email', 'Rank', 'FName', 'LName', 'Pass/Fail', 'SpmLic', 'Folders']
# open csv, write col heads close the csv
output_file = open("data.csv", "a", newline='')
writer = csv.writer(output_file)
writer.writerow(dict_labels)
output_file.close()
# for each file in the defined location check if its the correct file type
# if it is then run the 2 functions and write to csv
for file in os.listdir(files_location):
if file.endswith(file_extension):
output_file = open("data.csv", "a", newline='')
writer = csv.writer(output_file)
DeleteBlanks(file=file)
log_values = ReadLastLine(file,8)
writer.writerow(log_values)
output_file.close()