Python 从特定行写入文件
Python writing in files from a specific line
是否有 python 函数可以从特定行写入文件,我的意思是如果我知道该行的索引是否有可能从该行开始写入?
我认为没有任何方法可以按照您尝试的方式做到这一点
或者,将文件读入字符串,然后使用列表方法插入数据。
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)
你可以使用这个简单的过程
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)
是否有 python 函数可以从特定行写入文件,我的意思是如果我知道该行的索引是否有可能从该行开始写入?
我认为没有任何方法可以按照您尝试的方式做到这一点
或者,将文件读入字符串,然后使用列表方法插入数据。
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)
你可以使用这个简单的过程
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)