The issue I'm having is that instead over writing all of the lines of the read file to the output, only the last line is in the output file. I believe it is getting written over and over again, but I can't seem to tell how to fix the loop. If anyone could help me out, it'd be greatly appreciated. This issue may be with opening my file repeatedly in a for loop.
import os import re # 1.walk around directory and find lastjob.txt file in one of folders rootDir = "C:\\Users\Bob\Desktop\Path Parsing Project" for path, dirs, files in os.walk(rootDir): for filename in files: fullpath = os.path.join(path, filename) if filename=="text.txt": # 2.open file. read from file fi = open(fullpath, 'r') # 3.parse text in incoming file and use regex to find PATH for line in fi: m = re.search("(Adding file.*)",line) if m: #4.write path and info to outgoing file #print(line) fo = open('outputFile', 'w') fo.write(line + '\n')
fo = open('outputFile', 'w')->fo = open('outputFile', 'a'). You want append mode.