[英]How can I use Python 3 to write text to a file in Windows 10
The code I've tried is: 我尝试过的代码是:
dtf = open("C:\\Users\\foggi\\Documents\\Fred.txt","w")
dtf.write("Hello people")
This works fine on Android (with appropriate changes to the file path) but under Windows 10 it creates an empty file. 在Android上运行良好(对文件路径进行了适当的更改),但是在Windows 10下,它将创建一个空文件。 How can I overcome this?
我该如何克服?
You have to close the file, after you are done writing to it. 完成写入后,您必须关闭文件。
dtf = open("C:\\Users\\foggi\\Documents\\Fred.txt","w")
dtf.write("Hello people")
dtf.close()
Or, preferably: 或者,最好:
with open("C:\\Users\\foggi\\Documents\\Fred.txt","w") as dtf:
dtf.write("Hello people")
You can read more about the with
block, including how to create your own objects which you can use with with
, here: http://preshing.com/20110920/the-python-with-statement-by-example/ (first Google result). 您可以在以下位置阅读更多有关
with
块的信息,包括如何创建可与with
使用的对象: http : //preshing.com/20110920/the-python-with-statement-by-example/ (第一个Google结果)。
In theory, the file should close when its finalizer is run, but that's not guaranteed to happen. 从理论上讲,该文件应在其终结器运行时关闭,但不能保证会发生这种情况。 You need to close the file to flush its buffer to disk, otherwise the data you "wrote" could hang in your Python script's memory.
您需要关闭文件以将其缓冲区刷新到磁盘,否则“写入”的数据可能会挂在Python脚本的内存中。
The best way to ensure your file is closed is to use a with
block. 确保关闭文件的最佳方法是使用
with
块。
with open("C:\\Users\\foggi\\Documents\\Fred.txt", "w") as fp:
fp.write("Hello people")
Another good way is to just use pathlib
: 另一个好方法是只使用
pathlib
:
import pathlib
path = pathlib.Path("C:\\Users\\foggi\\Documents\\Fred.txt")
path.write_text("Hello people")
You could also close the file manually, but that's not recommended as a matter of style: 您也可以手动关闭文件,但是出于风格考虑,不建议这样做:
fp = open("C:\\Users\\foggi\\Documents\\Fred.txt", "w")
fp.write("Hello people")
fp.close()
The reason is because something might happen between open
and close
which would cause close
to get skipped. 原因是因为在
open
和close
之间可能会发生某些事情,这将导致close
被跳过。 In such a short program, it won't matter, but in larger programs you can end up leaking file handles, so the recommendation is to use with
everywhere. 在这么短的程序中,没关系,但是在较大的程序中,您最终可能会泄漏文件句柄,因此建议
with
任何地方使用。
The reason the file is empty is because you did not close the file object before opening it. 文件为空的原因是因为您没有在打开文件对象之前将其关闭。
It is recommended to open files with with
most of the time as it closes the file object for you behind the scene: 建议用打开文件
with
大多数的时间,因为它关闭你的文件对象幕后:
with open("C:\\Users\\foggi\\Documents\\Fred.txt","w") as f:
f.write("Hello people")
You need to close the file if you want to see the changes: 如果要查看更改,则需要关闭文件:
I use with
always, it closes the file when the block ends. 我始终使用
with
,它在块结束时关闭文件。
with open("C:\\Users\\foggi\\Documents\\Fred.txt","w") as fs:
fs.write("Hello people")
Or just close the file when you are finish writing. 或者在完成写入后仅关闭文件。 Use
file.close()
使用
file.close()
dtf = open("C:\\Users\\foggi\\Documents\\Fred.txt","w")
dtf.write("Hello people")
dtf.close()
dtf = open("C:\\Users\\foggi\\Documents\\Fred.txt","a+")
dtf.write("Hello people")
You can use this a+ as an alter 您可以使用此a +作为替代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.