Вы не можете сделать это так просто, как ожидалось. Одно из решений - добавить #
к соответствующей строке и переписать весь файл.
file.txt
:
A "Hello, World!" program generally is a computer program that outputs or displays the message "Hello, World!".
Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language.
It is often the first program written by people learning to code.[1][2] It can also be used as a sanity test to make sure that a computer
language is correctly installed, and that the operator understands how to use it
Hello world
main.py
:
fname = "file.txt"
with open(fname, "r") as f:
contents = f.readlines()
# Last line is at -1.
contents[-1] = '#' + contents[-1]
with open(fname, "w") as f:
f.write("".join(contents))
"".join(contents)
превращает список строк обратно в строку.