[英]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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.