Mar-08-2020, 05:41 AM
(This post was last modified: Mar-08-2020, 05:44 AM by Gribouillis.)
Sorry, I forgot the
It's all explained in the fileinput's module documentation
Within the 'with' block, writing to standard output will actually write to the target file. It gives a shorter code because we don't need to open the output file explicitly and the creation of the backup file is also handled by
fileinput.input(...)
in the first version. You need to import the fileinput module first.It's all explained in the fileinput's module documentation
documentation Wrote:Optional in-place filtering: if the keyword argument inplace=True is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.<some extension>'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.
Within the 'with' block, writing to standard output will actually write to the target file. It gives a shorter code because we don't need to open the output file explicitly and the creation of the backup file is also handled by
fileinput.input()
If there is no change, I think the file is simply copied.