ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/MW/CatLogFiles2.py
Revision: 591
Committed: Tue Nov 3 22:45:12 2015 UTC (10 years, 4 months ago) by nino.borges
Content type: text/x-python
File size: 2527 byte(s)
Log Message:
Moved dir out of main dir.

File Contents

# Content
1 ## CatLogFiles2.py
2 ## A simple Python program that will take preformatted log files (comma separated) from a
3 ## user selected set of Log_File directories (The user will select the scope) and Concatenate
4 ## their contents into one large Excel CSV file.
5 #Eborges
6 #11.12.02
7
8 import sys, os
9 from lgflcntr import getoffice
10
11 ## Function: CatLogFile
12 ## Purpose: Takes all log files in the directory inputed by the user, for each of the
13 ## App Servers chosen by the user, and concatenates the contents into a CSV file
14 ## on the executing user's U: drive.
15 ## Arguments: input_dir - Directory, given at the command prompt, that the program should
16 ## look in, to find all of the different log files it should concatenate
17 ## output_file - The name of the file that the program will store these CSV entries
18 ## in.
19 def catlogfile(input_dir, output_file):
20 #Determine the App Server scope.
21 appservers = getoffice()
22 output_file = "u:/" + output_file
23 print input_dir
24 print "Concatenating names and compiling them into one log file on your U:"
25 clear=open(output_file,'w')
26 clear.close()
27 #For each of the App Servers in the scope, grab a list of their contents.
28 for x in appservers:
29 server = x + '\n'
30 print server
31 directory = "/app/log_files/" + input_dir
32 place = "//" + x + directory
33 try:
34 FilesList = os.listdir(place)
35 #Read the contents of each of these files and put them into the CSV file chosen
36 #by the user.
37 for y in FilesList:
38 FullPath = place + "/" + y
39 f=open(FullPath, 'r')
40 contents = f.readlines()
41 f.close()
42 newfile=open(output_file,'a')
43 newfile.writelines(contents)
44 newfile.close()
45 except WindowsError:
46 #Catch the exception incase it dosent exist.
47 print x,"Does not have a log file... skipping"
48
49 print 'Finished! The new concatenated list has been written to %s'% output_file
50
51 if __name__ == '__main__':
52 if len(sys.argv) < 2:
53 print "Sorry but this program requires both an input directory and an output file.\nThe Syntax is\ncatlogfiles.py InputDirectory OutputFile"
54 else:
55 input_dir = sys.argv[1]
56 output_file = sys.argv[2]
57 catlogfile(input_dir, output_file)
58
59