在程序完成读取python之后,从头开始重新读取文件 [英] Re-read a file from start after the program finishes reading it python
问题描述
我有一个CSV文件,我在python中阅读。代码是这样的代码:
I have a CSV file which I am reading in python. The code is something along these lines:
with open("total.csv", 'rb') as input1:
reader = csv.reader(input1, delimiter=",")
for row in reader:
# Read the CSV row by row.
实际上,此文件包含许多URL,我希望检查更新,我希望我的程序从一旦我完成列表的文件的开始。如果我想停止它,我将手动杀死进程。但是,我不知道在读取最后一行后应该怎么做,以便程序应该从文件的开头开始。我想继续在循环中读取文件,以便读取文件,除非我手动终止运行我的脚本的进程。谁能告诉我该怎么做?感谢
Actually this file contains numerous URLs that I wish to check for updates and I want my program to start from the beginning of the file once I complete the list. If I want to stop it I will manually kill the process. However, I don't know what should I do after reading the last row so that the program should start from the beginning of the file. I want to keep on reading the file in a loop so that the file is read unless I manually terminate the process running my script. Can anyone tell how do I do that? Thanks
推荐答案
文件对象有 seek()
使用它来寻找0.但是,在读thd文件之前确保 time.sleep()
为一个短文件,否则程序会使CPU内核和磁盘IO饱和。
File objects have a seek()
method; use that to seek to 0. But make sure to time.sleep()
for a short file before reading thd file or your program will saturate a CPU core and disk IO.
或者更好的是,只需重新输入和
块即可重新打开文件。
Or better yet, just re-enter the with
block to open the file afresh.
这篇关于在程序完成读取python之后,从头开始重新读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!