如何在for循环中更改为不同的文件行

How to change to a different line of file in for loop

我有一个(写得不好的)ifs 和 elses 之类的长函数,它包含一个 for 循环以在每一行中查看文件:

def check(low,high):
    with open('users.txt', 'r+') as followed:
        ts = time.time()
        sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
        done_checking = open('done_checking.txt', "a")
        done = 0
        skippers = 0
        max_tries = 15
        for line in followed:
            if skippers==30:
                print "MAKE THE FOR LOOP GO BACK TO LINE 1 OF FILE"
            else:

                print "Does some other stuff"

                if done==max_tries:
                    sleep(randint(850,1150))
                    done = 0

                elif:
                    print: "SCRIPT DOES SOME STUFF!"

                else:
                    skippers += 1
                    print: "SCRIPT HAS SKIPPED A USER"                  

有没有办法像上面说的那样

"MAKE THE FOR LOOP GO BACK TO LINE 1 OF FILE"

我可以让 for 循环重置到它正在检查的文件的第一行 (users.txt) ?

(很明显,它打印的地方:"SCRIPT DOES SOME STUFF!",我有很多关于时间戳的乱七八糟的东西……)

"MAKE THE FOR LOOP GO BACK TO LINE 1 OF FILE"

是的,您需要调用搜索功能才能返回到您想要的行。

fileobject.seek(0)

这将返回到该特定文件的第 1 行。