首页 > 解决方案 > 如何将我的“打印内容”添加到文本文件中

问题描述

最近开始学习Python。

我定义了标题和价格。

现在我希望 Python 创建一个文本文件,并将“标题”和“价格”的内容输入到新创建的文本文件中

print(title)
print(price)

fh = open("Price&Title", "w")
fh.write()
fh.close()

上面的这段代码不起作用。

标签: pythonpython-3.x

解决方案


您可以使用以下file参数print

fh = open("Price&Title", "w")
print(title, file=fh)
print(price, file=fh)
fh.close()

推荐阅读