About 15,200,000 results
Open links in new tab
  1. How to Modify a Text File in Python - AskPython

    May 19, 2023 · Modifying a file means replacing a piece of text in the file with another piece of text. It’s a combination of read and write operations. To modify a file, we first have to open it, usually in r+ mode.

  2. python - How to modify a text file? - Stack Overflow

    Sep 24, 2008 · I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?

  3. Editing specific line in text file in Python - Stack Overflow

    Jan 18, 2011 · def replace_line(file_name, line_num, text): lines = open(file_name, 'r').readlines() lines[line_num] = text out = open(file_name, 'w') out.writelines(lines) out.close() And then: replace_line('stats.txt', 0, 'Mage')

  4. How to open and edit an existing file in Python? - Stack Overflow

    Dec 16, 2014 · The open() built-in Python method uses normally two arguments: the file path and the mode. You have three principal modes (the most used): r , w and a . r stands for read and will make the open("/path/to/myFile.txt", 'r') to open an existing file and to only be able to read it (not editing), with myFile.readlines() or other methods you can find ...

  5. Python Program to Replace Text in a File - GeeksforGeeks

    Sep 5, 2024 · In this article, we are going to replace Text in a File using Python. Replacing Text could be either erasing the entire content of the file and replacing it with new text or it could mean modifying only specific words or sentences within the existing text.

  6. Python: Inplace Editing using FileInput - GeeksforGeeks

    Feb 19, 2020 · It comes handy in many places but in this article, we’ll use the fileinput to do in-place editing in a text file. Basically we’ll be changing the text in a text file without creating any other file or overheads. Syntax: Note: The backup is extension for the backup file created before editing. Example 1: Changing only the first line of file.

  7. Python Program to Replace Specific Line in File

    Sep 14, 2021 · In this article, we are going to write a Python program to replace specific lines in the file. We will first open the file in read-only mode and read all the lines using readlines (), creating a list of lines storing it in a variable.

  8. How to Modify a Text File in Python? – Be on the Right Side of Change

    Feb 10, 2021 · Summary: You can modify a text file in Python using one of the following methods: Using The seek() Method; Using The fileinput Module; Using The splitlines() Method; Using the regex module and the split() and insert() methods; Overview. Problem: Given a text file; how to modify it in Python? Scenario 1: Insert a New Line in The File

  9. Advanced Text File Operations in Python

    Feb 18, 2025 · In Python, you can modify text files using a few key steps: Open the File. Use the open() function to open the file. Read or Write the File. Use file_object.write(text) to write text to the file. Use file_object.readlines() to read the file as a list of lines. Use file_object.read() to read the entire content as a string. Close the File.

  10. Python – How to Update and Replace Text in a File

    Aug 3, 2022 · Question: How would we write code to replace or update text in a file? We can accomplish this task by one of the following options: This method reads in a file, replaces existing text, then saves the file. For this example, each occurrence of Fals …

  11. Some results have been removed
Refresh