gpt4 book ai didi

python - io.UnsupportedOperation : not readable

转载 作者:太空狗 更新时间:2023-10-30 02:12:49 25 4
gpt4 key购买 nike

<分区>

我正在研究一个问题,它说要制作一个程序来获取文件的用户输入,然后在文件中删除用户指定的字符串。我不确定如何从我所拥有的(下面)到问题所要求的。一如既往,我们将不胜感激任何帮助。

def main():
outfile = open(input("Enter a file name: "), "a")
string = input("Enter the string to be removed: ")
for string in outfile.readlines():
string = string.replace(string, "")
outfile.close()
print("Done")

main()

我采纳了其中一项建议并尝试让它发挥作用,但正如我在下面的评论中所说,下面的代码不会返回错误,而是会创建一个空文件。我缺少什么才能使新文件成为删除字符串的旧文件?

def main():
inpath = input("Enter an input file: ")
line = input("Enter what you want to remove: ")
outpath = input("Enter an output file: ")
with open(inpath, "r") as infile, open(outpath, "w") as outfile:
for line in infile:
outfile.write(line.replace(line, "") + "\n")
print("Done.")

main()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com