📜  python 在位置写入文件 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:25.225000             🧑  作者: Mango

代码示例1
mystring = 'some string'
pos = 10

with open('/path/to/file.txt', 'r+') as f:
    contents = f.read()
    contents = contents[:pos] + mystring + contents[pos + 1:]
    f.seek(0)
    f.truncate()
    f.write(contents)