I'm not crazy about the existing solutions using rename
/remove
, because they oversimplify some of the file handling that the inplace
flag does - for example handling the file mode, handling a chmod
attribute, etc.
In my case, because I control the environment that my code is going to run in, I decided the only reasonable solution was to set my locale to a UTF8-using locale:
export LC_ALL=en_US.UTF-8
The effect is:
sh-4.2> python3.6 -c "import fileinput;
for line in fileinput.FileInput('DESCRIPTION', inplace=True): print(line.rstrip() + 'hi')
print('done')"
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "/usr/lib64/python3.6/fileinput.py", line 250, in __next__
line = self._readline()
File "/usr/lib64/python3.6/fileinput.py", line 364, in _readline
return self._readline()
File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 227: ordinal not in range(128)'
sh-4.2> export LC_ALL=en_US.UTF-8
sh-4.2> python3.6 -c "import fileinput;
for line in fileinput.FileInput('DESCRIPTION', inplace=True): print(line.rstrip() + 'hi')
print('done')"
done
sh-4.2#
The potential side-effects are changes to other file input & output, but I'm not worried about that here.