首页 > 解决方案 > Python脚本无限迭代文本文件

问题描述

需要帮助尝试使此脚本从开始到 EOF 并返回到同一进程无限行运行。这是代码。

with open('views.txt', 'r') as f:   
    
    if f == EOFError:
               
        f.seek(0, os.SEEK_SET)
        for x, line in enumerate(f):
            line.strip()
            
            link = r'https://www.youtube.com/watch?v={}'.format(line)

            wb.register('chrome',None,wb.BackgroundBrowser(
                                    "C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
            wb.get('chrome').open(link, new=0, autoraise=True)
            # time.sleep(randint(200,350))
            time.sleep(randint(10, 13))
            os.system("taskkill /im chrome.exe /f")
            time.sleep(randint(5,7))
                

    if f != EOFError:
        for x, line in enumerate(f):
            line.strip()
            
            link = r'https://www.youtube.com/watch?v={}'.format(line)

            wb.register('chrome',None,wb.BackgroundBrowser(
                                    "C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
            wb.get('chrome').open(link, new=0, autoraise=True)
            # time.sleep(randint(200,350))
            time.sleep(randint(10, 13))
            os.system("taskkill /im chrome.exe /f")
            time.sleep(randint(5,7))
                
                            
    else:
        pass

标签: python

解决方案


使用 Python 你可以这样做:

while 1:
    for line in open('views.txt').readlines():
        line = line.strip()
        link = r'https://www.youtube.com/watch?v={}'.format(line)
        ...
        os.system("taskkill /im chrome.exe /f")
        time.sleep(randint(5,7))

推荐阅读