I am adjusting the formatting of my output file. I want the text 'The total count is ...' to be printed at the very beginning of the file (since I could end up with a few thousand lines). Currently it is printing it at the very end of the file.
I have ran open () has both 'a' and 'w'. When I use 'w' it will seek to the beginning, but it will write over the text instead of moving the text. When I use 'a', the output.seek(0, 0) doesn't move to the beginning.
count = 0
with open('found_%s_or_not.txt' % replaceString, 'a') as output:
for root, subdir, files in os.walk(scanFolder):
for file in files:
if os.path.join(file).endswith('.mpr'):
fullpath = os.path.join(root, file)
with open(fullpath, 'r') as f:
for lines in file:
line = f.read()
if replaceString in line:
output.write(os.path.join(root, file) + ' contains ' + replaceString + '\n')
count = count + 1
output.seek(0, 0)
output.write('The total count is ' + str(count) + '\n\n')
I would like it to look like this:
The total count is 2
c:\spam contains bacon
c:\pig contains bacon
Currently I am getting this:
c:\spam contains bacon
c:\pig contains bacon
The total count is 2