0

Can someone tell me how can I only count the new appended lines or text in my file if it has already an old contents? I only want to start counting when I added a new line.

Shimizuu
  • 1
  • 1
  • Does this answer your question? [How do you append to a file?](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file) – Alderven Nov 01 '21 at 14:01
  • Does the file already exist? Where is the new contents coming from? Are you asking how to keep track of the number to add at the start of the file? – doctorlove Nov 01 '21 at 14:02
  • I already know how to append in a file but it doesn't show the numbers. I want to know how can I also show it in the output. – Shimizuu Nov 01 '21 at 14:03
  • @doctorlove Yes, the file already exist and the new contents is from the my input. And yes, I want to keep track of the number when I am adding something to the file – Shimizuu Nov 01 '21 at 14:05

1 Answers1

1

Given some values (from input or similar):

values = [['mine ','@@jk','FB'], ['yours','kkkk','Twitter']]

you can enumerate them and use the number from that:

for number, value in enumerate(values):
    print(f'{number+1} {" ".join(values)}')

Instead of print, you may want to write straight to the file, but the enumerate method gives you the running count you wanted. It starts at 0, so add one as you loop.

doctorlove
  • 18,872
  • 2
  • 46
  • 62