再次读取文件Python
当文件到达末尾时,我想一次又一次地阅读它。
该文件只是用逗号分隔的数字。
我使用 python,我在文档中读到 file.seek(0) 可以用于此目的,但对我不起作用。
这是我的脚本:
self.users = []
self.index = -1
infile = open(filename, "r")
for line in infile.readlines():
if line != None:
self.users.append(String.split((line),','))
else:
infile.seek(0)
infile.read()
infile.close()
self.index= self._index +1
return self.users[self.index]
谢谢您的帮助
I would like to read a file again and again when it arrives at the end.
The file is only numbers separate by comma.
I use python and I read on the doc that file.seek(0) can be use for this but doesn't work for me.
This is my script:
self.users = []
self.index = -1
infile = open(filename, "r")
for line in infile.readlines():
if line != None:
self.users.append(String.split((line),','))
else:
infile.seek(0)
infile.read()
infile.close()
self.index= self._index +1
return self.users[self.index]
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
infile.read()
将读入整个文件,然后丢弃结果。为什么要这样做?当您调用
infile.readlines
时,您已经读入了整个文件。然后循环迭代结果,这只是一个 Python 列表。移动到文件的开头不会对此产生任何影响。如果您的代码实际上在到达末尾后确实移动到文件的开头,那么它只会永远循环,直到耗尽内存(因为
users
列表不断增长)。您可以通过将
readlines()
的结果存储在变量中,然后将整个for 行放入 all_lines:
循环来获得您所要求的行为另一个while True:
。 (或者每次关闭、重新打开并重新读取,如果 (a) 您担心该文件可能被另一个程序更改,或者 (b) 您想避免一次性读取所有内容。对于 (b )您可以将for line in infile.readlines():
替换为for line in infile:
。对于 (a),请注意尝试读取文件时可能会执行其他操作。无论你怎么做,写入它都可能是一个坏主意。)我强烈怀疑你所要求的行为并不是你真正想要的。您试图通过让程序不断读取文件来实现的目标是什么?
infile.read()
will read in the whole of the file and then throw away the result. Why are you doing it?When you call
infile.readlines
you have already read in the whole file. Then your loop iterates over the result, which is just a Python list. Moving to the start of the file will have no effect on that.If your code did in fact move to the start of the file after reaching the end, it would simply loop for ever until it ran out of memory (because of the endlessly growing
users
list).You could get the behaviour you're asking for by storing the result of
readlines()
in a variable and then putting the wholefor line in all_lines:
loop inside anotherwhile True:
. (Or closing, re-opening and re-reading every time, if (a) you are worried that the file might be changed by another program or (b) you want to avoid reading it all in in a single gulp. For (b) you would replacefor line in infile.readlines():
withfor line in infile:
. For (a), note that trying to read a file while something else might be writing to it is likely to be a bad idea no matter how you do it.)I strongly suspect that the behaviour you're asking for is not what you really want. What's the goal you're trying to achieve by making your program keep reading the file over and over?
“else”分支永远不会被执行,因为 for 循环将迭代文件的所有行,然后退出。
如果您希望执行查找操作,则必须将其放在 for 循环之外。
问题是,如果您永远循环,您将耗尽内存。如果您只想读取两次,则复制并粘贴 for 循环,否则决定退出条件并使用中断操作。
The 'else' branch will never be pursued because the for loop will iterate over all the lines of the files and then exit.
If you want the seek operation to be executed you will have to put it outside the for loop
The problem is, if you will loop for ever you will exhaust the memory. If you want to read it only twice then copy and paste the for loop, otherwise decide an exit condition and use a break operation.
readlines 已经将整个文件内容读取到内存列表中,您可以自由地一次又一次地迭代!
要重新读取文件,请执行以下操作:
readlines
is already reading the entire file contents into an in-memory list, which you are free to iterate over again and again!To re-read the file do:
您可以在此处使用 itertools.cycle()。
这是一个例子:
You can use itertools.cycle() here.
Here's an example :