0

I have just recently started coding in python and found myself overwhelmed when it came to creating this script.

So, I have a chatlog.txt file that is constantly being updated and I want to make it so that whenever a new line that contains "Apple" (for example) appears, something happens (ex. print("Green")). Of course, this would be a loop and the exit conditions would be when "Apple" has appeared five times.

chatlog.txt

Adam received a pear
Adam received a pear
Adam received a tomato       # This is the chatlog.txt file when it is first opened
Adam received an orange
Adam received a banana

All of the lines when the chatlog.txt file should be ignored and only the ones that come them should be taken into account.

A̶d̶a̶m̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶a̶ ̶p̶e̶a̶r̶
A̶d̶a̶m̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶a̶ ̶p̶e̶a̶r̶
A̶d̶a̶m̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶a̶ ̶t̶o̶m̶a̶t̶o̶
A̶d̶a̶m̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶a̶n̶ ̶o̶r̶a̶n̶g̶e̶
A̶d̶a̶m̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶a̶ ̶b̶a̶n̶a̶n̶a̶
**Adam received an apple**     -> This triggers Messages()

Messages

```python
import pyautogui as sendstuff

def Messages():
    sendstuff.press("enter")
    sendstuff.typewrite("That is a nice apple!")
    sendstuff.press("enter")
```

How do I do this?

TheBig89
  • 1
  • 1
  • 4
    Hi! I recommend to include: 1) Your attempt code 2) An example of your txt file 3) And the Errors or misbehaviors that you are finding while executing your code. Please refer to these guidelines [how to ask a good question?](https://stackoverflow.com/help/how-to-ask). If you follow the guidelines I guarantee that more people will be able to help you, thanks! – EnriqueBet Apr 05 '20 at 07:12
  • 1
    BTW, I don't recommend using an infinite for loop. As it could happen that your condition will never be met. Instead, I'll recommend creating a job in your OS task manager that executes this script with frequency X, in that way if your condition is not met in a period of time fixed by you, the code will finish and retry later. – EnriqueBet Apr 05 '20 at 07:15

0 Answers0