【发布时间】:2010-12-21 02:39:57
【问题描述】:
如何在Python中设置当前工作目录?
【问题讨论】:
如何在Python中设置当前工作目录?
【问题讨论】:
试试os.chdir
os.chdir(path)将当前工作目录更改为路径。可用性:Unix、Windows。
【讨论】:
os.getcwd(),这显示了os.chdir()接受的输入格式。
也许这就是你要找的:
import os
os.chdir(default_path)
【讨论】:
import os
print os.getcwd() # Prints the current working directory
设置工作目录:
os.chdir('c:\\Users\\uname\\desktop\\python') # Provide the new path here
【讨论】:
\u 是语法错误;那应该是\\u。
getcwd)......了不起。
使用 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')
【讨论】:
os.chdir 命令对 panda 的使用有所不同?
os.path.dirname(__file__),例如
它也适用于 Mac
import os
path="/Users/HOME/Desktop/Addl Work/TimeSeries-Done"
os.chdir(path)
检查工作目录
os.getcwd()
【讨论】:
Pythonpython的核心思想之一就是跨平台工作。
"~/etc" 没有成功。
~,则需要使用os.path.expanduser("~/etc"),然后会扩展为完整路径(/path/to/homedir/etc)