在python中更改当前工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20796355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:15:19  来源:igfitidea点击:

change current working directory in python

python

提问by Tunafish Ha

I made a folder on my desktop with the name "headfirstpython" and I need to change my current working directory to that folder and to the sub folder inside of it. I used os.getcwd() to get the current folder and it gives me 'C\Python32'. I used os.chdir('../headfirstpython/chapter3') to change the directory but its telling it cannot find the path

我在桌面上创建了一个名为“headfirstpython”的文件夹,我需要将当前工作目录更改为该文件夹及其内部的子文件夹。我使用 os.getcwd() 来获取当前文件夹,它给了我“C\Python32”。我使用 os.chdir('../headfirstpython/chapter3') 来更改目录,但它告诉它找不到路径

>>> import os
>>> os.getcwd()
'C:\Python32'
>>> os.chdir('../headfirstpython/chapter 3')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
os.chdir('../headfirstpython/chapter 3')
WindowsError: [Error 3] The system cannot find the path specified:         '../headfirstpython/chapter 3'
>>> os.chdir('../headfirstpython/chapter3')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
os.chdir('../headfirstpython/chapter3')
WindowsError: [Error 3] The system cannot find the path specified:   '../headfirstpython/chapter3'
>>> 

采纳答案by Brandon

I think a few things may be helpful.

我认为一些事情可能会有所帮助。

It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders.

看起来您使用的是 Windows 系统,因此您应该使用双反斜杠 '\\' 来分隔文件夹。

Second, if you're trying to change to a folder within the current folder, you should use a single dot, and not two, e.g. os.chdir('.\\folder')

其次,如果您尝试更改到当前文件夹中的文件夹,您应该使用一个点,而不是两个,例如 os.chdir('.\\folder')

Finally, if the folder you are trying to access is not a direct subfolder of the current working directory (or otherwise in your path), you need to include the full path to access it. Since you said it's on your desktop, you'd probably want something that looks like this:

最后,如果您尝试访问的文件夹不是当前工作目录的直接子文件夹(或路径中的其他文件夹),您需要包含完整路径才能访问它。既然你说它在你的桌面上,你可能想要这样的东西:

import os
os.chdir('C:\Users\username\Desktop\headfirstpython') ## Where username is replaced with your actual username

From here, you could also change directories to the chapter3 subdirectory with the following

从这里,您还可以使用以下命令将目录更改为 Chapter3 子目录

os.chdir('chapter3') 

Which is equivalent in this case with

在这种情况下相当于

os.chdir('.\chapter3')

or, if you want to be wordy:

或者,如果你想罗嗦:

os.chdir('C:\Users\username\Desktop\headfirstpython\chapter3')

Hopefully that helps?

希望这有帮助吗?

回答by Jane

I had the same problem before.I solve this when I found that if I created a file on my desktop, the file image would be shown on my desktop, but it will not exist in C/users/Desktop. Maybe you can check whether your file is exist in your C drive's desktop or not. Hope this will help.

我之前也遇到过同样的问题,我解决了这个问题,发现如果我在桌面上创建了一个文件,文件图像会显示在我的桌面上,但它不会存在于C/users/Desktop中。也许您可以检查您的文件是否存在于您的 C 盘桌面。希望这会有所帮助。