![]() |
File Merging - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: File Merging (/thread-10571.html) |
File Merging - vino - May-25-2018 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 |