当前工作目录到底是什么? [英] What exactly is Current working directory?

查看:688
本文介绍了当前工作目录到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的书说:

计算机上运行的每个程序都有一个当前的工作目录或cwd.假定所有不以根文件夹开头的文件名或路径都在当前工作目录下

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上,我的根文件夹是/.当我在Python shell中键入os.getcwd()时,得到/Users/apple/Documents.为什么我的cwd中有Documents文件夹?它是说Python正在使用Documents文件夹吗?是否没有以/(根文件夹)开头的指向Python的路径?另外,每个程序都有不同的cwd吗?

As I am on OSX, my root folder is /. When I type in os.getcwd() in my Python shell, I get /Users/apple/Documents. Why am I getting the Documents folder in my cwd? Is it saying that Python is using Documents folder? Isn't there any path heading to Python that begins with / ( the root folder ) ? Also, does every program has a different cwd?

推荐答案

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

Your python interpreter location is based off of how you launched it, as well as subsequent actions taken after launching it like use of the os module to navigate your file system. Merely starting the interpreter will place you in the directory of your python installation (not the same on different operating systems). On the other hand, if you start by editing or running a file within a specific directory, your location will be the folder of the file you were editing. If you need to run the interpreter in a certain directory and you are using idle for example, it is easiest to start by creating a python file there one way or another and when you edit it you can start a shell with Run > Python Shell which will already be in that directory. If you are using the command line interpreter, navigate to the folder where you want to run your interpreter before running the python/python3/py command. If you need to navigate manually, you can of course use the following which has already been mentioned:

import os
os.chdir('full_path_to_your_directory')

这篇关于当前工作目录到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆