【问题标题】:Python writing in files from a specific linePython 从特定行写入文件
【发布时间】:2015-05-07 14:28:20
【问题描述】:

是否有 python 函数可以从特定行写入文件,我的意思是如果我知道该行的索引是否有可能从该行开始写入?

【问题讨论】:

  • 是的,有可能。

标签: python input printing output


【解决方案1】:

我认为没有任何方法可以按照您尝试的方式做到这一点

或者,将文件读入字符串,然后使用列表方法插入数据。

source_file = open("myfile", "r")
file_data = list(source_file.read())
source_file.close()
file_data.insert(position, data)
open("myfile", "wb").write(file_data)

【讨论】:

    【解决方案2】:

    你可以使用这个简单的过程

    source_file = open("file.txt", "r")
    file_data = list(source_file.read())
    source_file.close()
    file_data.insert(position, data)
    open("file.txt", "wb").write(file_data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多