This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: fileinput inplace clobbers file without leaving backup on decode errors
Type: behavior Stage:
Components: IO Versions: Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: switchnode
Priority: normal Keywords:

Created on 2017-06-19 04:14 by switchnode, last changed 2022-04-11 14:58 by admin.

Messages (1)
msg296304 - (view) Author: switchnode (switchnode) Date: 2017-06-19 04:14
Consider the script:

$ cat
#!/usr/bin/env python
import fileinput
srt = fileinput.input(inplace=True)
print(srt.readline(), end='')
for line in srt:
        print(line, end='')

Called on text files, it will do nothing.

$ ls -alh test.*
-rw-r--r-- 1 501 utmp 1.3G Jun 18 22:17 test.mp4
-rw-r--r-- 1 501 utmp  71K Jun 18  2017
$ ./
$ ls -alh test.*
-rw-r--r-- 1 501 utmp 1.3G Jun 18 22:17 test.mp4
-rw-r--r-- 1 501 utmp  71K Jun 18  2017

However, if the user accidentally supplies the filename of a video instead of the associated srt...

$ ./ test.mp4
Traceback (most recent call last):
  File "./", line 4, in <module>
    print(srt.readline(), end='')
  File "/usr/lib/python3.6/", line 299, in readline
    line = self._readline()
  File "/usr/lib/python3.6/", line 364, in _readline
    return self._readline()
  File "/usr/lib/python3.6/", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 43: invalid start byte
$ ls -alh test.*
-rw-r--r-- 1 501 utmp    0 Jun 18  2017 test.mp4
-rw-r--r-- 1 501 utmp  71K Jun 18  2017
$ ls -alh * | grep 'bak'

Oops! It is gone.

I'm not sure why this happens. (Without the context-manager syntax, I would expect the program to end by excepting, fail to close the FileInput, and leave the backup file behind—certainly that would be the merciful option.)
Date User Action Args
2022-04-11 14:58:47adminsetgithub: 74885
2020-09-21 23:10:20iritkatrielsetcomponents: + IO
2017-06-19 04:14:47switchnodecreate