requirement: i have a directory , that contains multiple sub directories, each sub directory has multiple text and log files, my script fetches the required lines from all the sub directories and stores it in one text file.
but i want it to store separate text file for each sub directory ,after fetching the contents. can anyone please help me where to edit my script. my script is currently dumping all in on file instead of separate file for each directory.
import zipfile,fnmatch,os import os, shutil, re, glob from os.path import isfile, join from os import walk root_src_dir = r'E:\\New folder' root_dst_dir = "E:\\destination" for src_dir, dirs, files in os.walk(root_src_dir): dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1) if not os.path.exists(dst_dir): os.makedirs(dst_dir) for file_ in files: src_file = os.path.join(src_dir, file_) dst_file = os.path.join(dst_dir, file_) if os.path.exists(dst_file): os.remove(dst_file) shutil.copy(src_file, dst_dir) rootPath = r"E:\\destination" pattern = '*.zip' for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): print(os.path.join(root, filename)) zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0])) os.chdir(rootPath) for file in glob.glob(pattern): f = open((file.rsplit(".", 1)[0]) + "ANA.txt", "w") f.close() ##here folder output mypath =r"E:\\destination" newpath = os.path.expanduser("E:\\destination") filenam = "1.txt" f = [] path = '' path1 = [] for (dirpath, dirnames, filenames) in walk(mypath): if isfile(join(mypath, dirpath)): path1.extend(join(mypath, dirpath)) if os.path.isdir(join(mypath, dirpath)): for f in filenames: path1.append(str(join(mypath, dirpath, f))) print(path1) newf = open(os.path.join(newpath, filenam ), "w+") myarray = {"ERROR", "error"} for element in myarray: elementstring = ''.join(element) for f in path1: openfile = open(os.path.join(path, f), "r") for line in openfile: if elementstring in line: newf.write(line) newf.close() openfile.close()
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor