0

i tried to make auto check if file is modified then print and send to discord using webhook, but it print the last line and before the last line. i just want to print the last line. here is the code

def readfile():
    with open("test.txt", "r") as t:
        read = t.readlines()
    return read

before = readfile()
while True:
    after = readfile()
    if before != after:
        for line in after:
            if line not in before:
                print(line)
                sendembed()
        before = after

im new to python, anyone can help me?

Nitro
  • 1
  • 1
    Does this answer your question? [How do I watch a file for changes?](https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes) – MoonMist Jun 29 '22 at 03:23

1 Answers1

0

given that the first part is answered with the link: How do I watch a file for changes?

This will answer the second part, which the question was close on.

def readfile():
  
    with open(`\test.txt`, "r") as t:
        read = t.readlines()
    return read


x = readfile()
# this is the last line
print( x[-1] )

The reason for this is that readlines() creates a list and the user can access the last element of the list.

D.L
  • 4,339
  • 5
  • 22
  • 45