Don't empty the file when it is supposed to be only read.

Iterating over an `fileinput.input()` object with `inplace=True`
and not returning any results per iteration leaves an empty
input file.

Solves #18680