【问题标题】:How to create and write to a text file in python [duplicate]如何在python中创建和写入文本文件[重复]
【发布时间】:2020-10-27 09:55:32
【问题描述】:

如何使用 python 在 python 中创建和写入文本文件,以及如何将这个文本文件保存到我的计算机?我希望仅使用 Python 即可访问此文本文件,并且该文本文件中的所有内容都可以在控制台中访问。

我还希望将此文件保存在 Documents 目录中。

问题是没有给出输出,也没有在同一目的地创建任何文本文件。

这是我运行的代码:

myfile = open("newfile.txt", "w")
myfile.write("This is text from newfile")
myfile.close()

【问题讨论】:

  • 不清楚。你是什​​么意思:“该文本文件中的所有内容都可以在控制台中访问”?它与“保存在 Documents 目录中”有什么关系?

标签: python command-line


【解决方案1】:

此代码将在 .py 的位置创建一个文件

myfile = open("Greetings.txt", 'w')
myfile.write("Hello")
myfile.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2019-07-13
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多