用单独的程序替换另一个文件中的文本

2024-04-25 03:50:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个文件:

text

program.py

我将文本插入我的

text

使用以下行创建文件:

inp=input('Text by the user')
with open("text.py", "a") as myfile:
   myfile.write(inp)

如何使程序删除文本中的行,使其显示:

text2

不是

text1 text2

什么时候跑两次


Tags: 文件thetextpy文本inputbywith
2条回答

更少的代码

with open("text.txt", "w") as file:
    file.write(input("Write on file> "))

以写入模式(w)而不是追加模式(a)打开。这将在写入文件之前清空该文件

inp=input('Text by the user')
with open("text.py", "w") as myfile:
   myfile.write(inp)

相关问题 更多 >