Inplace Editing using Python FileInput

Inplace editing is a technique that allows us to modify the contents of a file directly, without creating a new file or loading the entire file into memory. Python's fileinput module provides a streamlined approach to file manipulation by allowing changes directly to the existing file, making it an efficient and resource-friendly method.

The fileinput module, part of the Python standard library, provides a high-level interface for reading and writing files. With this module, we can open a file for inplace editing, iterate over its lines, make modifications, and save changes directly to the file.

Importing the Fileinput Module

Before using the fileinput module, we need to import it into our Python script ?

import fileinput

Opening a File for Inplace Editing

To open a file for inplace editing, we use the fileinput.input() function with the inplace=True parameter. This returns a FileInput object that can be used in a loop to access each line ?

# Create a sample file first
with open('example.txt', 'w') as f:
    f.write("This is a sample file.\n")
    f.write("It contains some foo words.\n")
    f.write("Let's perform inplace editing using Python.\n")

# Display original content
with open('example.txt', 'r') as f:
    print("Original content:")
    print(f.read())
Original content:
This is a sample file.
It contains some foo words.
Let's perform inplace editing using Python.

Example 1: Adding a Prefix to Each Line

We can add a prefix to each line using inplace editing ?

import fileinput

# Reset the file content
with open('example.txt', 'w') as f:
    f.write("This is a sample file.\n")
    f.write("It contains some foo words.\n")
    f.write("Let's perform inplace editing using Python.\n")

# Add prefix to each line
with fileinput.input('example.txt', inplace=True) as f:
    for line in f:
        modified_line = "Prefix: " + line
        print(modified_line, end='')

# Display modified content
with open('example.txt', 'r') as f:
    print("After adding prefix:")
    print(f.read())
After adding prefix:
Prefix: This is a sample file.
Prefix: It contains some foo words.
Prefix: Let's perform inplace editing using Python.

Example 2: Replacing Text

We can replace specific words in the file using the replace() method ?

import fileinput

# Reset the file content
with open('example.txt', 'w') as f:
    f.write("This is a sample file.\n")
    f.write("It contains some foo words.\n")
    f.write("Let's perform inplace editing using Python.\n")

# Replace 'foo' with 'bar'
with fileinput.input('example.txt', inplace=True) as f:
    for line in f:
        modified_line = line.replace('foo', 'bar')
        print(modified_line, end='')

# Display modified content
with open('example.txt', 'r') as f:
    print("After replacing 'foo' with 'bar':")
    print(f.read())
After replacing 'foo' with 'bar':
This is a sample file.
It contains some bar words.
Let's perform inplace editing using Python.

Example 3: Converting to Uppercase

We can convert all text to uppercase using the upper() method ?

import fileinput

# Reset the file content
with open('example.txt', 'w') as f:
    f.write("This is a sample file.\n")
    f.write("It contains some foo words.\n")
    f.write("Let's perform inplace editing using Python.\n")

# Convert to uppercase
with fileinput.input('example.txt', inplace=True) as f:
    for line in f:
        modified_line = line.upper()
        print(modified_line, end='')

# Display modified content
with open('example.txt', 'r') as f:
    print("After converting to uppercase:")
    print(f.read())
After converting to uppercase:
THIS IS A SAMPLE FILE.
IT CONTAINS SOME FOO WORDS.
LET'S PERFORM INPLACE EDITING USING PYTHON.

Key Points

  • Always use inplace=True parameter in fileinput.input()
  • Use print(modified_line, end='') to preserve original line endings
  • The end='' argument prevents adding extra newline characters
  • Use a with statement for proper file handling
  • Changes are written directly to the original file

Comparison of Methods

Method Memory Usage Best For
Inplace editing Low (line by line) Large files
Read all + write High (entire file) Small files
Create new file High (two files) Backup needed

Conclusion

The fileinput module provides an efficient way to modify files directly without loading entire contents into memory. Use inplace=True with proper print() statements to preserve file formatting. Exercise caution as changes are permanent to the original file.

Updated on: 2026-03-27T12:28:02+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements