1

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

Carlos Gonzalez
  • 858
  • 13
  • 23
  • 1
    In `a` mode, you can only write at the end. In `w` mode, you can seek and write anywhere, but it will overwrite anything that's there already. NO operating system that I've ever heard of has the ability to *insert* data at arbitrary places in a file. You could possibly write a bunch of spaces at the start of the file when you open it, so that you can overwrite them later without damaging any of your actual data. – jasonharper Jun 11 '19 at 13:12
  • An easy way to prevent much load on head is to use list of lines by 'readlines' method. Then modify them the way you want, and then by opening the file with 'w+', just use 'writelines', to write that list in the file. – Vicrobot Jun 11 '19 at 13:12
  • „it will write over the text instead of moving the text“ That is literally what writing to a file means. It is how files work. If you want content moved, you have to do that yourself. – MisterMiyagi Jun 11 '19 at 13:12
  • This line -- output.seek(0, 0) -- is used with an append mode, so you cannot write to the top of the file. – Life is complex Jun 11 '19 at 14:29
  • Possible duplicate of [How can I prepend to the top of the file?](https://stackoverflow.com/questions/55519989/how-can-i-prepend-to-the-top-of-the-file) – Life is complex Jun 11 '19 at 14:51

1 Answers1

0

Instead of trying to add to the file incrementally, try making a list of lines and then writing them all at once:

output_list = []
with open('found_%s_or_not.txt' % replaceString, 'w') as output:
    for root, subdir, files in os.walk(scanFolder):
        ...                             
                        if replaceString in line:                     
                            output_list.append(os.path.join(root, file) + ' contains ' + replaceString + '\n') 
        ...
    output_list = ['The total count is ' + str(count) + '\n\n'] + output_list
    output.write(''.join(output_list))
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53