python - 我的 Python 3x 代码有什么问题?

标签 python python-3.x

我有这样的代码,

database = open("C:\\Users\\PC\\Desktop\\database.txt", "w")
enter=input("What is your name: ")
database.write(enter)
database.close

当我运行这个程序时,它不会在 database.txt 中打印任何内容

我尝试使用 IDLE 和 PyScripter,但结果相同 :(

最佳答案

database.close 更改为 database.close()。您没有调用该方法。

我建议为此使用 Python 的上下文管理器。该文件将自动为您关闭。

with open("C:\\Users\\PC\\Desktop\\database.txt", "w") as database:
    enter = input("What is your name: ")
    database.write(enter)        

关于python - 我的 Python 3x 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751944/

相关文章:

python-3.x - 如何修复错误 “module ' cv2.cv 2' has no attribute setMouseCallBack?”

python - cython:不允许超出主包的相对 cimport

python - 如何记录 ConfigParser 的内容?

python-3.x - pydub 没有找到 ffprobe

python - 精度可变的百分比

python - 如何在样条插值的所有数据点处设置导数零约束?

Python 缩进错误 : unindent does not match any outer indentation level

python - 将十六进制转换为 bin 并放入另一个数组 Python 3

python - 远程服务的 Django 服务,不返回 PATCH 请求的响应

python - 通过多处理减少内存占用?