再次读取文件Python

发布于 2024-10-24 07:09:47 字数 461 浏览 2 评论 0原文

当文件到达末尾时,我想一次又一次地阅读它。

该文件只是用逗号分隔的数字。

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风流物 2024-10-31 07:09:47
  1. infile.read() 将读入整个文件,然后丢弃结果。为什么要这样做?

  2. 当您调用infile.readlines时,您已经读入了整个文件。然后循环迭代结果,这只是一个 Python 列表。移动到文件的开头不会对此产生任何影响。

  3. 如果您的代码实际上在到达末尾后确实移动到文件的开头,那么它只会永远循环,直到耗尽内存(因为 users 列表不断增长)。

  4. 您可以通过将 readlines() 的结果存储在变量中,然后将整个 for 行放入 all_lines: 循环来获得您所要求的行为另一个while True:。 (或者每次关闭、重新打开并重新读取,如果 (a) 您担心该文件可能被另一个程序更改,或者 (b) 您想避免一次性读取所有内容。对于 (b )您可以将 for line in infile.readlines(): 替换为 for line in infile:。对于 (a),请注意尝试读取文件时可能会执行其他操作。无论你怎么做,写入它都可能是一个坏主意。)

  5. 我强烈怀疑你所要求的行为并不是你真正想要的。您试图通过让程序不断读取文件来实现的目标是什么?

  1. infile.read() will read in the whole of the file and then throw away the result. Why are you doing it?

  2. 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.

  3. 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).

  4. You could get the behaviour you're asking for by storing the result of readlines() in a variable and then putting the whole for line in all_lines: loop inside another while 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 replace for line in infile.readlines(): with for 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.)

  5. 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?

荒路情人 2024-10-31 07:09:47

“else”分支永远不会被执行,因为 for 循环将迭代文件的所有行,然后退出。
如果您希望执行查找操作,则必须将其放在 for 循环之外。

self.users = []
self.index = -1
infile = open(filename, "r")
while True:
    for line in infile.readlines():
        self.users.append(String.split((line),','))
    infile.seek(0)             
infile.close()
self.index= self._index +1 
return self.users[self.index]

问题是,如果您永远循环,您将耗尽内存。如果您只想读取两次,则复制并粘贴 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

self.users = []
self.index = -1
infile = open(filename, "r")
while True:
    for line in infile.readlines():
        self.users.append(String.split((line),','))
    infile.seek(0)             
infile.close()
self.index= self._index +1 
return self.users[self.index]

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.

自找没趣 2024-10-31 07:09:47

readlines 已经将整个文件内容读取到内存列表中,您可以自由地一次又一次地迭代!

要重新读取文件,请执行以下操作:

infile = file('whatever')
while True:
    content = infile.readlines()

    # do something with list 'content'

    # re-read the file - why? I do not know
    infile.seek(0)

infile.close()

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:

infile = file('whatever')
while True:
    content = infile.readlines()

    # do something with list 'content'

    # re-read the file - why? I do not know
    infile.seek(0)

infile.close()
囍孤女 2024-10-31 07:09:47

您可以在此处使用 itertools.cycle()。
这是一个例子:

import itertools
f = open(filename)
lines = f.readlines()
f.close()
for line in itertools.cycle(lines):
    print line,

You can use itertools.cycle() here.
Here's an example :

import itertools
f = open(filename)
lines = f.readlines()
f.close()
for line in itertools.cycle(lines):
    print line,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文