Skip to content
Related Articles

Related Articles

Improve Article

in_place module in Python

  • Last Updated : 18 Jul, 2021
Geek Week

Sometimes, while working with Python files, we can have utility in which we wish to alter files without using system context or sys stdout. Such situation require reading/writing files inplace, i.e without using process resources. This can be achieved using the ein_place module in Python. This module –

  • Doesn’t use sys.stdout, rather returns the new filehandler instance.
  • Supports all filehandler methods.
  • In case of exception, the original file is retained, helping in atomicity.
  • Creation of temporary files doesn’t hit other files opened.

Installation 

Simply run this command to install the required module

pip install in_place

Now to actually get the job done use in_place() method of this module.

Syntax:



in_place.InPlace(filename, mode=t, backup=None, backup_ext=None, delay_open=False, move_first=False)

Parameter:

filename : Location of file to open and edit inplace.
mode : Mode with which to open file. Options are byte (b) and text(t). In case its open in byte more, read and write happens in bytes. In text mode, R & W happens in string. Defaults to text mode.
backup : Path to save original file after the end of process in case to keep backup of original file.
backup_ext : Created backup of with name – filename + backup_ext, of original file before editing. 
delay_open : In case set to True, it will only open file after calling open(), whereas by default its open as soon as class is initiated.
move_first : If set to True, the input file is sent to  temporary location and output is edited in place first. By default this process takes place after calling of close() or when process ends.

Using this function the file in use is first opened and a required task is performed on it. Given below is the simplest implementation for the same.

Example 1 :

Input:

Original Content

Python3




import in_place
  
# using inplace() to perform same file edit.
with in_place.InPlace('gfg_inplace') as f:
    for line in f:
  
        # reversing cases while writing in file, inplace
        f.write(''.join(char.upper() if char.islower() else char.lower()
                        for char in line))

Output :



Output of file after code execution.- Alters cases.

It is also possible to first back up the data of the existing file onto some other file and then make the required changes to the file. For this the name of the file by which you want to create a back-up is given as an value to backup argument.

Example 2 : 

Input:

Output of file after code execution.- Alters cases.

Python3




import in_place
  
# using inplace() to perform same file edit.
# backs up original file in given path.
with in_place.InPlace('gfg_inplace', backup='origin_gfg_inplace') as f:
    for line in f:
  
        # reversing cases while writing in file, inplace
        f.write(''.join(char.upper() if char.islower() else char.lower()
                        for char in line))

Output : 

After code execution, inplace changes.

Backup of original contents is created in new file.

The alternative to the above method is the use fo keyword backup_ext. Onto this just pass the extra word to differentiate the existing file from.

Example 3:

Input:

Python3




import in_place
  
# using inplace() to perform same file edit.
# adds extension to file and creates backup
with in_place.InPlace('gfg_inplace', backup_ext='-original') as f:
    for line in f:
  
        # reversing cases while writing in file, inplace
        f.write(''.join(char.upper() if char.islower() else char.lower()
                        for char in line))

Output : 

After code execution, inplace changes.

Created backup of original file, adding given extension.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :