python - 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 你可以这样做:
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))
推荐阅读
- c++ - 将 QString 从父级传递给子级 QT
- azure - 从 ARM 模板获取 API 管理的公共 IP 地址
- c# - 使用 Contains 或 IndexOf 方法检查值的状态或索引时如何从文本框中检索值?
- visual-studio-code - 如何使用 Visual Studio Code 在美人鱼的甘特时间轴中添加日期
- r - r - 如何增加 ggplot2 中面板背景的大小
- python - 如何在 Flask Web 应用程序中使用现有的 PostGIS 数据库
- php - 在 PHP 中运行的脚本之间存储变量值的最佳方法?
- reactjs - 如何让 webpack 开发服务器检查客户端是桌面还是移动端并响应不同的 html?
- ipc - 消息队列与 TCP IP 套接字。哪个IPC在linux中更快?
- html - 嵌套在 CSS 网格中