相关疑难解决方法(0)

如何解决"OSError:通过next()调用告诉位置被禁用"

我正在创建一个文件编辑系统,并希望创建一个基于行的tell()函数而不是基于字节的函数.此函数将在打开(文件)调用的"with循环"内使用.此函数是具有以下内容的类的一部分:

self.f = open(self.file, 'a+')
# self.file is a string that has the filename in it
Run Code Online (Sandbox Code Playgroud)

以下是原始函数(如果您想要行和字节返回,它还有一个char设置):

def tell(self, char=False):
    t, lc = self.f.tell(), 0
    self.f.seek(0)
    for line in self.f:
        if t >= len(line):
            t -= len(line)
            lc += 1
        else:
            break
    if char:
        return lc, t
    return lc
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,这会返回一个OSError,它与系统如何迭代文件有关,但我不明白这个问题.感谢任何能提供帮助的人.

python error-handling file-io next

15
推荐指数
3
解决办法
1万
查看次数

标签 统计

error-handling ×1

file-io ×1

next ×1

python ×1