gpt4 book ai didi

python - 当前工作目录到底是什么?

转载 作者:行者123 更新时间:2023-11-28 20:36:13 27 4
gpt4 key购买 nike

我的书说:

Every program that runs on your computer has a current working directory, or cwd. Any filenames or paths that do not begin with the root folder are assumed to be under the current working directory


因为我在 OSX 上,所以我的根文件夹是/。当我输入 os.getcwd()在我的 Python shell 中,我得到 /Users/apple/Documents .为什么我的 cwd 中有 Documents 文件夹?是说 Python 正在使用 Documents 文件夹吗?没有任何以 / 开头的通往 Python 的路径吗? (根文件夹)?另外,每个程序都有不同的 cwd 吗?

最佳答案

您的 python 解释器位置取决于您启动它的方式,以及启动它后采取的后续操作,例如使用 os 模块来导航您的文件系统。仅仅启动解释器就会把你放在你的 python 安装目录中(在不同的操作系统上不一样)。另一方面,如果您从编辑或运行特定目录中的文件开始,您的位置将是您正在编辑的文件的文件夹。如果您需要在某个目录中运行解释器并且您正在使用 idle 例如,最容易通过一种或另一种方式创建一个 python 文件开始,当您编辑它时,您可以使用 Run > Python Shell 启动一个 shell将已经在该目录中。如果您使用的是命令行解释器,请在运行 python/python3/py 命令之前导航到要运行解释器的文件夹。如果您需要手动导航,您当然可以使用已经提到的以下内容:

import os
os.chdir('full_path_to_your_directory')

关于python - 当前工作目录到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com