Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sub command
#4
Sorry, I forgot the 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.
Reply


Messages In This Thread
sub command - by Skaperen - Mar-07-2020, 07:26 PM
RE: sub command - by Gribouillis - Mar-07-2020, 11:16 PM
RE: sub command - by Skaperen - Mar-08-2020, 01:32 AM
RE: sub command - by Gribouillis - Mar-08-2020, 05:41 AM
RE: sub command - by buran - Mar-08-2020, 07:20 AM
RE: sub command - by Gribouillis - Mar-08-2020, 09:48 AM
RE: sub command - by ndc85430 - Mar-08-2020, 11:01 AM
RE: sub command - by Skaperen - Mar-08-2020, 10:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020