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') 
4
  • 4
    fo = open('outputFile', 'w') -> fo = open('outputFile', 'a'). You want append mode. Commented Jul 12, 2015 at 23:35
  • see: stackoverflow.com/questions/4706499/… Commented Jul 12, 2015 at 23:37
  • 1
    Why are you opening the file repeatedly? Open it once, at the start. Commented Jul 12, 2015 at 23:37
  • @Scott Hunter I changed the location of my file to the start, and my application was ridiculously faster at processing. Commented Jul 12, 2015 at 23:39

1 Answer 1

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() 
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to close it at the end!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.