【问题标题】:Python command for setting working directory to source file location in Spyder用于在 Spyder 中将工作目录设置为源文件位置的 Python 命令
【发布时间】:2019-02-28 01:35:31
【问题描述】:

我想要一个 python 代码行,将工作目录设置为代码所在的文件夹。我正在使用 spyder IDE 编写和运行 python 代码。

旁注:这个问题与R command for setting working directory to source file location in Rstudio非常相似

【问题讨论】:

    标签: python spyder


    【解决方案1】:

    这是我在 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)
    

    【讨论】:

      猜你喜欢
      • 2012-11-20
      • 1970-01-01
      • 2022-06-21
      • 2016-11-18
      • 2017-11-16
      • 2015-04-17
      • 2013-06-03
      • 2012-03-24
      • 2018-06-03
      相关资源
      最近更新 更多