【问题标题】:Reading from a frequently write math function to new line file [duplicate]从经常写入的数学函数读取到换行文件[重复]
【发布时间】:2021-08-08 14:35:03
【问题描述】:

可以用这段代码写下一行吗? 怎么做? 我想读(n)行,然后做数学写(n+1),无限循环。

import time
def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

if __name__ == '__main__':
    logfile = open("myfile.txt","r+")
    loglines = follow(logfile)
    for line in loglines:
        write.logfile ("5*2+1\n")

【问题讨论】:

  • @MauriceMeyer 但它只工作 1 次,我需要循环不停读第 n 行,然后写 n + 1。
  • 请分享你的代码,否则没人知道你在说什么
  • @MauriceMeyer 这是我的新问题,谢谢stackoverflow.com/questions/68702899/…

标签: python


【解决方案1】:

您可以通过使用文件对象的方法来实现。

这里是链接:https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

您可以使用seekwriteread 来实现您想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多