1

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')
pHorseSpec
  • 1,246
  • 5
  • 19
  • 48

1 Answers1

1

By placing fo = open('outputFile', 'w') at the beginning, I got the desired result and the script processed much faster.

import os
import re
fo = open('outputFile', 'w')
# 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(r'(Adding file.*)',line)
            if m:
                fo.write(line)
fo.close()
fi.close()
pHorseSpec
  • 1,246
  • 5
  • 19
  • 48