Write at a Specific Position in a File in Python

In Python, you can write at a specific position in a file using the seek() method, which moves the file pointer to a desired position before writing. The tell() method can help track the current position in the file. Depending on whether you want to overwrite or insert text, you may need to read, modify, and rewrite the content.


Examples to Write at a Specific Position in a File

1. Overwriting Text at a Specific Position in a File

To overwrite text at a specific position, we open the file in r+ mode, use seek() to move the cursor, and then write new content.

In this example, we open example.txt, seek the file pointer to 5th byte, and then insert some text.

main.py

</>
Copy
# Opening file in read & write mode
with open("example.txt", "r+") as file:
    file.seek(5)  # Move to the 5th character
    file.write("INSERT")  # Overwriting text

# Reading updated content
with open("example.txt", "r") as file:
    print(file.read())

Explanation:

  1. open("example.txt", "r+"): Opens the file in read-write mode.
  2. seek(5): Moves the file pointer to the 5th byte (character).
  3. write("INSERT"): Overwrites content from this position.
  4. open("example.txt", "r"): Reopens the file to display the modified content.

Output:

The contents of example.txt with the updated content.

2. Inserting Text Without Overwriting in a File

To insert text at a specific position without overwriting, read the content, modify it, and write it back.

main.py

</>
Copy
# Reading file content
with open("example.txt", "r") as file:
    content = file.read()

# Insert text at a specific position
position = 5
new_content = content[:position] + " INSERT " + content[position:]

# Writing modified content back
with open("example.txt", "w") as file:
    file.write(new_content)

# Reading updated content
with open("example.txt", "r") as file:
    print(file.read())

Explanation:

  1. read(): Reads the entire file content.
  2. content[:position] + " INSERT " + content[position:]: Inserts the text at index 5 without losing existing data.
  3. open("example.txt", "w"): Opens the file in write mode to replace content.

Output:

The contents of example.txt with the updated content.

3. Appending Text at the End of a Specific Line in a File

To modify a specific line by appending text, read all lines, modify the target line, and rewrite the file.

main.py

</>
Copy
# Read all lines
with open("example.txt", "r") as file:
    lines = file.readlines()

# Modify a specific line
line_number = 1  # Modify the second line (index 1)
lines[line_number] = lines[line_number].strip() + " - APPENDED TEXT\n"

# Write back to file
with open("example.txt", "w") as file:
    file.writelines(lines)

# Reading updated content
with open("example.txt", "r") as file:
    print(file.read())

Explanation:

  1. readlines(): Reads all lines into a list.
  2. lines[line_number].strip() + " - APPENDED TEXT\n": Modifies the second line.
  3. writelines(lines): Writes the updated list of lines back to the file.

Output:

The contents of example.txt with the updated content.

4. Replacing a Specific Word in a File

To replace a word at a specific position, read content, modify it using replace(), and write back.

main.py

</>
Copy
# Read file content
with open("example.txt", "r") as file:
    content = file.read()

# Replace a specific word
modified_content = content.replace("World", "Python")

# Write modified content back
with open("example.txt", "w") as file:
    file.write(modified_content)

# Reading updated content
with open("example.txt", "r") as file:
    print(file.read())

Explanation:

  1. read(): Reads the file content.
  2. replace("World", "Python"): Replaces occurrences of “World” with “Python”.
  3. write(modified_content): Writes the updated content back.

Output:

The contents of example.txt with the updated content.

Conclusion

In this tutorial, we covered multiple ways to write at a specific position in a file in Python:

  1. Using seek(): Directly move the pointer to a specific position for overwriting.
  2. Reading and Modifying Content: Read, modify, and write back to insert text.
  3. Editing a Specific Line: Read lines into a list, modify one, and rewrite the file.
  4. Replacing a Specific Word: Use replace() to find and modify specific words.