python - Move file to new directory only if it contains specified string -
i have 1 folder thousands of files , need loop through every single file , see if file contains specific string, once has concluded has specific string, must moved correct folder. far have:
for filename in glob.iglob('*.txt'): f = open(filename) s = mmap.mmap(f.fileno(), 0, access=mmap.access_read) if s.find('* test outcome : fail') != -1: src_file = os.path.join(dirstart, filename) dst_file = os.path.join(dirfail, filename) shutil.move(src_file, dst_file + filename)
at moment, moves first file know fact there's more. thanks
from re import compile pattern = compile("\* test outcome : fail") filename in glob.iglob('*.txt'): fl = open(filename, 'r') in fl.readlines(): if pattern.search(i): fl.close() src_file = os.path.join(dirstart, filename) dst_file = os.path.join(dirfail, filename) shutil.move(src_file, dst_file + filename) break #to stop checking other lines
Comments
Post a Comment