duc hathaway
duc hathaway

Reputation: 467

Creating while loop based on txt length?

So basically I am trying to create a loop that checks the length of a txt file. I am running a separate script to write to the text file while this script is running. When the length in the txt file is longer than 5 I want to break and progress to the rest of the script. Seems like i need to have the script check the txt file each cycle, but unsure of how to do this. How do i set this up?

CODE-

import time


def start():
    with open('test.txt') as token_text:
        token = token_text.read().splitlines()
        response_token = (token[0])
        while len(response_token) <= 5:
            try:
                print("Waiting for captcha")
                time.sleep(1)
            except:
                print('captcha ready')
                break   

    print("Script Start")  

start()

Upvotes: 0

Views: 57

Answers (1)

Danil Speransky
Danil Speransky

Reputation: 30473

I would create a function to obtain the token length:

def token_length():
    with open('test.txt') as file:
        return len(file.read().splitlines()[0])

while token_length() <= 5:
    print('waiting')
    time.sleep(1)

print('ready')

Upvotes: 1

Related Questions