【问题标题】:what's the default folder for new files in python? [duplicate]python中新文件的默认文件夹是什么? [复制]
【发布时间】:2020-02-27 21:10:48
【问题描述】:

您好,我是学习 python 的新手,我正在学习 python 文件处理。 以下代码工作正常。但是我找不到 python 创建这个文件的文件夹。 (我正在使用 Visual Studio 代码)

f = open("demofile2.txt", "a") 
f.write("Now the file has new content ")
f.close()


f = open("demofile2.txt", "r") 
print(f.read())

【问题讨论】:

  • 你检查过运行python脚本的文件夹吗?
  • "文件是一个类似路径的对象,给出了路径名(绝对或相对于当前工作目录)" - 在发布之前,您应该始终阅读docs for the code you're using

标签: python file location file-handling


【解决方案1】:

您的代码创建的文件应与您的 python 代码位于同一位置

【讨论】:

  • 非常感谢。我找到了
【解决方案2】:

如果你运行这个,你会知道文件保存在哪里:

import os

print(os.getcwd())

【讨论】:

  • 不知道,谢谢!
【解决方案3】:

当您尝试通过代码打开或创建文件时,解释器将始终指向您的当前/当前工作目录。

使用下面一行通过你的 py 文件了解你的密码。

os.getcwd()

通过终端(OSX/Linux):

pwd

通过 CMD (Windows):

cd

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2011-03-31
    • 2011-07-06
    相关资源
    最近更新 更多