0

GOAL

I have a text file which receives receives at least 20 more lines every seconds. What I want is that every time a new line appears in my txt file, I want the line to go through a process. For example, the line will be uppercase and print it out. And then wait for the next line to appear.

WHAT I TRIED

My idea was to read the file like we can normally read a file line by line in python because by the time it was reading the lines, some more lines would be added. However, python reads the lines too fast... This was my code:

file = open('/file/path/text.txt')
for line in file:
     line = line.upper
     print(line)
     time.sleep(1)

I know there is PySpark, but is there an easier solution? Because I only have one text file and it must go through a simple process (eg: upper case read lines, print line and then wait for next line to appear).

I am a newbie in python so I am sorry if the answer is obvious for some of you. I would appreciate any help. Thank you.

lolaa
  • 181
  • 1
  • 4
  • 11
  • What does "Python reads the lines too fast" mean? You will basically need to trap the end of file and continue reading. – tripleee Aug 15 '20 at 12:28
  • What I mean is that when I start the program it finishes to read all the lines before some new lines appear. What does you mean I would need to trap the end of file and continue reading? @tripleee – lolaa Aug 15 '20 at 12:29
  • This can be helpful as well https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python – Matus Dubrava Aug 15 '20 at 12:33

0 Answers0