【发布时间】:2019-02-28 01:35:31
【问题描述】:
我想要一个 python 代码行,将工作目录设置为代码所在的文件夹。我正在使用 spyder IDE 编写和运行 python 代码。
旁注:这个问题与R command for setting working directory to source file location in Rstudio非常相似
【问题讨论】:
我想要一个 python 代码行,将工作目录设置为代码所在的文件夹。我正在使用 spyder IDE 编写和运行 python 代码。
旁注:这个问题与R command for setting working directory to source file location in Rstudio非常相似
【问题讨论】:
这是我在 Jupyter 中进行命令行开发时遇到的常见问题。
你可以试试这个来找到你的脚本是从哪里执行的:
import os
from pathlib import Path
def myPath():
'''
return the current working directory in both interpeters and when exectued on the commandline
'''
try:
# path of this file when executed
wd = os.path.dirname(os.path.abspath(__file__))
except NameError as e:
print('this script is running in an interpreter')
# if not found
wd = Path().resolve()
return(wd)
【讨论】: