Package: in-place
GitHub|PyPI|Issues|ChangelogThein_placemodule provides anInPlaceclass for reading & writing a
file “in-place”: data that you write ends up at the same filepath that you read
from, andin_placetakes care of all the necessary mucking about with
temporary files for you.For example, given the filesomefile.txt:'Twas brillig, and the slithy toves
    Did gyre and gimble in the wabe;
All mimsy were the borogoves,
    And the mome raths outgrabe.and the programdisemvowel.py:importin_placewithin_place.InPlace("somefile.txt")asfp:forlineinfp:fp.write("".join(cforcinlineifcnotin"AEIOUaeiou"))after running the program,somefile.txtwill have been edited in place,
reducing it to just:'Tws brllg, nd th slthy tvs
    Dd gyr nd gmbl n th wb;
ll mmsy wr th brgvs,
    nd th mm rths tgrb.and no sign of those pesky vowels remains!  If you want a sign of those pesky
vowels to remain, you can instead save the file’s original contents in, say,somefile.txt~by constructing the filehandle with:in_place.InPlace("somefile.txt",backup_ext="~")or save tosomeotherfile.txtwith:in_place.InPlace("somefile.txt",backup="someotherfile.txt")Compared to the in-place filtering implemented by the Python standard library’sfileinputmodule,in_placeoffers the following benefits:Instead of hijackingsys.stdout, a new filehandle is returned for
writing.The filehandle supports all of the standard I/O methods, not justreadline().There are options for setting the encoding, encoding error handling, and
newline policy for opening the file, along with support for opening files in
binary mode, and these options apply to both input and output.The complete filename of the backup file can be specified; you aren’t
constrained to just adding an extension.When used as a context manager,in_placewill restore the original file
if an exception occurs.The creation of temporary files won’t silently clobber innocent bystander
files.Installationin_placerequires Python 3.8 or higher.  Just usepipfor Python 3 (You have pip, right?) to install it:python3 -m pip install in_placeBasic Usagein_placeprovides a single class,InPlace.  Its constructor takes the
following arguments:name=<PATH>(required)The path to the file to open & edit in-placemode=<"b"|"t"|None>Whether to operate on the file in binary or text mode.  Ifmodeis"b", the file will be opened in binary mode, and data will be read &
written asbytesobjects.  Ifmodeis"t"orNone(the
default), the file will be opened in text mode, and data will be read &
written asstrobjects.backup=<PATH>If set, the original contents of the file will be saved to the given path
when the instance is closed.backupcannot be set to the empty string.backup_ext=<EXTENSION>If set, the path to the backup file will be created by appendingbackup_extto the original file path.backupandbackup_extare mutually exclusive.backup_extcannot
be set to the empty string.**kwargsAny additional keyword arguments (such asencoding,errors, andnewline) will be forwarded toopen()when opening both the input and
output file streams.name,backup, andbackup_extcan bestr, filesystem-encodedbytes, or path-like objects.InPlaceinstances act as read-write filehandles with the usual filehandle
attributes, specifically:__iter__()              __next__()              closed
flush()                 name                    read()
read1() *               readinto() *            readinto1() *
readline()              readlines()             write()
writelines()

* binary mode onlyInPlaceinstances also feature the following new or modified attributes:close()Close filehandles and move files to their final destinations.  If called
after the filehandle has already been closed,close()does nothing.Be sure to always close your instances when you’re done with them by callingclose()orrollback()either explicitly or implicitly (i.e., via use
as a context manager).rollback()Likeclose(), but discard the output data (keeping the original file
intact) instead of replacing the original file with it__enter__(),__exit__()When anInPlaceinstance is used as a context manager, on exiting the
context, the instance will be either closed (if all went well) or rolled
back (if an exception occurred).InPlacecontext managers are notreusablebut arereentrant(as long as no further operations are performed
after the innermost context ends).inputThe actual filehandle that data is read from, in case you need to access it
directlyoutputThe actual filehandle that data is written to, in case you need to access it
directly