I wrote a script that finds the maximum value in a log file. I can then write the max value to another file. My problem: a. is how do I run it against all the files in a directory b. write "Filename" + "Max Value" into 1 file.
here's my code:
1 import re
2
3 a = 'NA total MB July.csv'
4 b = 'Total.csv'
5
6 with open(a, 'r') as f1:
7 with open(b,'w') as f2:
8 header = f1.readline()
9 data= f1.readlines()
10 pattern= re.compile(",|\s")
11 maxMB=[]
12 for line in data:
13 parts = pattern.split(line)
14 #print "Log line split",parts #splits the number
15 mbCount= parts[2] #index of the number
16 mbint=float(mbCount)
17 maxMB.append(mbint)# creates a list of all MBs
18 #print "MAX: ", maxMB #prints out the max MB
19 highest=max(maxMB)
20 print highest
21 f2.write(str(highest))#writes highest value to file
Here's my files output
167.94
What I'm looking to see in Total.csv is
NA total MB July : 167.94
NA total MB August: 123.45
...
..
. For all the csv files with in a folder
Can't quite figure out how to make this work without processing 1 file at a time and manually changing the file name. Any help to this n00b would be greatly appreciated. THANKS!
You can use os.listdir()
to grab files in current directory.
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
#do something
You can open Total.csv
file in ab
mode so that you can append all the max values into that one file only.
with open('Total.csv', 'ab') as out:
writer = csv.writer(out, delimiter=',', quotechar='"',quoting=csv.QUOTE_ALL)
row = (,)
writer.writerow(row)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.