Home  >  Q&A  >  body text

file - Python how to read the file content and write it back to the original file after modification

How to PythonRead File contentAfter modifying WriteReturn to the original file

给我你的怀抱给我你的怀抱2669 days ago1042

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-06-28 09:27:30

    Reference article: Issues related to Python file operations

    Modify the file content and write back the original file

    with open(filename) as f:
        file_str = f.read()
        
    # do stuff with file_str
    
    with open(filename, 'w') as f:
        f.write(file_str)

    reply
    0
  • Cancelreply