I am trying to append some text to the beginning of a file in python, however rather than appending text to the file, it creates a new file with the same name, and writes to that file.
#!/usr/bin/env python3
import os
files = 'f0_ascii'
x = 0
for file in os.listdir(files):
x += 1
with open(file, 'a') as file2:
y = 0
for line in file:
y += 1
file2.seek(0, 0)
string_in_string = "sometext {}".format(y - 10)
file2.write(string_in_string)
file2.close()
if x == 1:
exit()
I want it to append the beginning of the existing file with "sometext {}".format(y-10)