how to write a line in a text file

Wade wade at leftwich.us
Mon Jul 25 19:45:35 EDT 2005


Steven D'Aprano wrote:
>I'm usually opposed to creeping featuritis in programming languages ("it
>would be really cool if Python had a built-in command to do my entire
>application") but safe over-writing of files does cry out for a "batteries
>included" approach:


How about the fileinput module?

http://docs.python.org/lib/module-fileinput.html

"""
Optional in-place filtering: if the keyword argument inplace=1 is
passed to 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 keyword argument
backup='.<some extension>' is also given, 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.
"""




More information about the Python-list mailing list