Feature request: method to cancel or bail out from fileinput.input(inplace=True)

Sparr Risher sparr0 at gmail.com
Tue May 5 18:07:58 EDT 2020


After using https://docs.python.org/3/library/fileinput.html to open a file for inplace filtering, a backup is created before output is redirected to the new file. It is possible, but non-trivial and non-obvious, to bail out of this situation, putting the backed up file back in place and ending the output redirection.

My request is for a .cancel() or similar method that will automate this bail out process, leaving the filesystem and streams in the state they were in before fileinput.input() was called.

The primary use case for this functionality would be upon encountering an exception in the middle of file processing.

While on the subject, I also believe the following additional functionality would be worthwhile, even without fulfillment of the main request:

A method to return the extension provided for the backup file, and/or to return the full name of the backup file.

A parameter to disallow silent overwrite of the backup file.


More information about the Python-list mailing list