bpo-31281: Fix pathlib.Path incompatibility in fileinput by zmwangx · Pull Request #3208 · python/cpython (original) (raw)

fileinput otherwise works fine with pathlib.Path filenames, but when inplace is set to True, the Path object needs to be converted to a str first, or appending a str suffix with + would fail:

import fileinput import pathlib with fileinput.input(files=(pathlib.Path('in.txt'),), inplace=True) as fp: for line in fp: print(line, end='')

=>

Traceback (most recent call last): File "./pathlib-fileinput.py", line 6, in for line in fp: File "/Users/zmwang/.pyenv/versions/3.6.1/lib/python3.6/fileinput.py", line 250, in next line = self._readline() File "/Users/zmwang/.pyenv/versions/3.6.1/lib/python3.6/fileinput.py", line 331, in _readline self._filename + (self._backup or ".bak")) TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

https://bugs.python.org/issue31281