Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 File Merging
#1
Hi All,

I am a newbie for python and need some help in the below program code.

Explanation:
We have a script which perform few checks the server and produces log file as below
abc1serv-1-<date>.log
abc1serv-2-<date>.log
abc1serv-3-<date>.log

def2sserv-1-<date>.log
def2sserv-2--<date>.log
def2sserv-3-<date>.log

The above server are tagge's to a group in the file '/Script/Checks/CE_LIST.txt' as below
abc-1
def-2

Requirement:
Find for file named "abc1*-<date>.log" and "def2*-<date>.log in the folder '/Script/Check/Logs' then merge the file as

abc1serv-1-<date>.log , abc1serv-2-<date>.log ,abc1serv-3-<date>.log as "abc-1-<date>.log"
def2sserv-1-<date>.log, def2sserv-2--<date>.log, def2sserv-3-<date>.log as "def-2-<data>.log"

Issue:
The below code is able to find the file nad merge the file as above , but is merges additional data such as

The file "abc-1-<date>.log" contains the data of the file's "abc1serv-1-<date>.log , abc1serv-2-<date>.log ,abc1serv-3-<date>.log"

The file "def-2-<data>.log" contain the data of all the file "abc1serv-1-<date>.log , abc1serv-2-<date>.log ,abc1serv-3-<date>.log, def2sserv-1-<date>.log, def2sserv-2--<date>.log, def2sserv-3-<date>.log" instead of just the content of the file "def2sserv-1-<date>.log, def2sserv-2--<date>.log, def2sserv-3-<date>.log".

import os
import datetime
import fnmatch

cEpath = '/Script/Checks/CE_LIST.txt'
lPath = '/Script/Check/Logs'

def Content():
    fContent = []
    mList = []
    mData = []
    with open(cEpath,'r') as fp:
         for line in fp:
             fContent.append(line.strip().lower().replace("-" , "") + "*" + "-" +str(datetime.date.today()) + ".log")
    for i in fContent:
        for dName, sdName, fList in os.walk(lPath):
            for fName in fList:
                if fnmatch.fnmatch(fName, i):
                   with open(os.path.join(dName, fName),'r') as fData:
                        mData.append(fData.read())
                        with open(os.path.join(lPath, i.replace("*", "")), 'w') as sFile:
                             sFile.write("".join(mData))
                             sFile.close()
    
Content()
From,
Vino.B
micseydel wrote May-25-2018, 05:41 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)