How do I use (if they exist) string placeholders for writing in a text file? I want to write a text file, which keeps track of how many lines are written in itself. As soon as the amount of lines changes, the count should change accordingly.
Here is an example:
number_of_lines = 0
with open('./test.txt', "w") as out_file:
out_file.write('number of elements in list: '+str(2)+'\n')
out_file.write('element 1\n')
out_file.write('element 2\n')
with open('./test.txt', "r+") as out_file:
next(out_file)
out_file.write('element 3\n')
out_file.write('element 4\n')
out_file.write('element 5\n')
out_file.write('element 6\n')
out_file.write('element 7\n')
out_file.write('element 8\n')
out_file.write('element 9\n')
out_file.write('element 10\n') # now 2 Digits '10'
out_file.seek(0)
for i in out_file:
number_of_lines += 1
out_file.seek(0)
out_file.write('number of elements in list: '+str(number_of_lines-1)+'\n')
text file:
number of elements in list: 10
lement 1
element 2
element 3
element 4
element 5
element 6
element 7
element 8
element 9
element 10