简体   繁体   English

如何在Windows 10中使用Python 3将文本写入文件

[英]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. 原因是因为在openclose之间可能会发生某些事情,这将导致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.

相关问题 如何通过 Python 在 Windows 10 上使用 Unix? - How can use Unix on Windows 10 by Python? 为 Windows 编写一个脚本文件(.bat 或 .cmd),这样我就可以在任何 Windows 10 台预装 Python 的 PC 上自动运行现有的 Django 项目 - Write a script file (.bat or .cmd) for Windows, so I can autorun an existing Django Project on any Windows 10 PC with Python preinstalled 如何将打印 output 写入 Python 中的文本文件? - How can I write the print output to a text file in Python? 如何在Python 2.7中改进文本文件中的写入流 - How can I improve write streaming in a text file in Python 2.7 如何使用 python 从坐标写入 dxf 文件? - How can I use python to write a dxf file from coordinates? 如何强制 Python 的 file.write() 在 Windows 中使用与 Linux 中相同的换行符格式(“\\r\\n”与“\\n”)? - How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n” vs. “\n”)? 如何通过 PowerShell 在 python 中读取文本文件? 视窗 10 - How do I read a text file in python via PowerShell? Windows 10 如何在 python 3 中写入文本文件? - How can you write to a text file in python 3? 为什么我不能在 Python (Windows 10) 中使用这个字符串 unicodeescape? - Why can't I use this string unicodeescape in Python (Windows 10)? 如何使用 Python 导航 Windows 10 UI 以更改特定设置? - How can I use Python to navigate Windows 10 UI to change a specific setting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM