638

如何在 Python 中设置当前工作目录?

4

5 回答 5

909

尝试os.chdir

os.chdir(path)

        将当前工作目录更改为路径。可用性:Unix、Windows。

于 2009-11-27T21:59:59.637 回答
147

也许这就是您正在寻找的:

import os
os.chdir(default_path)
于 2009-11-27T22:00:09.700 回答
60
import os
print os.getcwd()  # Prints the current working directory

设置工作目录:

os.chdir('c:\\Users\\uname\\desktop\\python')  # Provide the new path here
于 2016-01-24T03:41:21.167 回答
13

它也适用于Mac

import os
path="/Users/HOME/Desktop/Addl Work/TimeSeries-Done"
os.chdir(path)

检查工作目录

os.getcwd()
于 2017-08-31T07:14:29.663 回答
-11

使用 pandas 包的人

import os
import pandas as pd

tar = os.chdir('<dir path only>') # do not mention file name here
print os.getcwd()# to print the path name in CLI

以下语法用于在 python CLI 中导入文件

dataset(*just a variable) = pd.read_csv('new.csv')
于 2016-12-22T06:12:56.530 回答