我正试着写一些文本到一个文件,下面是我试过的:
text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \
"industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \
" when an unknown printer took a galley of type and scrambled it to make a type specimen book."
target = open("file", 'wb')
target.writelines(text)
我得到一个空文件。 我该怎么做?
以下是如何打印到txt文件:
另一种方法是:
这是一个我用来编写txt文件的程序:
通过这种方式,您应该直接关闭文件:
这样,在
with
完成执行后,在缩进块之后关闭文件:更多信息:
writelines
需要一个iterable(例如一个列表)行,所以不要使用它。您需要关闭文件以保存更改,最好使用with
语句:相关问题 更多 >
编程相关推荐