【问题标题】:Find path of python_notebook.ipynb when running it with Google Colab使用 Google Colab 运行时查找 python_notebook.ipynb 的路径
【发布时间】:2021-02-12 16:24:14
【问题描述】:

我想找到存储我的 CODE 文件的 cwd。

有了 jupiter Lab 我会这样做:

import os 
cwd= os.getcwd()
print (cwd)
OUT: 
'C:...\\Jupiter_lab_notebooks\\CODE'

但是,如果我将文件夹复制到我的 GoogleDrive,并在 GOOGLE COLAB 中运行笔记本,我会得到:

import os 
cwd= os.getcwd()
print (cwd)
OUT: 
/content

无论我的笔记本存放在哪里。 如何找到 .ipynb 文件夹的实际存储路径?

#编辑

我正在寻找的是 python 代码,它将返回 COLAB 笔记本的位置,无论它存储在驱动器的哪个位置。这样我就可以从那里导航到子文件夹。

【问题讨论】:

    标签: python path jupyter-notebook google-colaboratory getcwd


    【解决方案1】:

    这个问题困扰了我一段时间,这个代码应该设置工作目录如果笔记本已经被奇异发现,仅限于Colab系统和挂载的驱动器,这个可以在Colab上运行:

    import requests
    import urllib.parse
    import google.colab
    import os
    
    google.colab.drive.mount('/content/drive')
    
    
    def locate_nb(set_singular=True):
        found_files = []
        paths = ['/']
        nb_address = 'http://172.28.0.2:9000/api/sessions'
        response = requests.get(nb_address).json()
        name = urllib.parse.unquote(response[0]['name'])
    
        dir_candidates = []
    
        for path in paths:
            for dirpath, subdirs, files in os.walk(path):
                for file in files:
                    if file == name:
                        found_files.append(os.path.join(dirpath, file))
    
        found_files = list(set(found_files))
    
        if len(found_files) == 1:
            nb_dir = os.path.dirname(found_files[0])
            dir_candidates.append(nb_dir)
            if set_singular:
                print('Singular location found, setting directory:')
                os.chdir(dir_candidates[0])
        elif not found_files:
            print('Notebook file name not found.')
        elif len(found_files) > 1:
            print('Multiple matches found, returning list of possible locations.')
            dir_candidates = [os.path.dirname(f) for f in found_files]
    
        return dir_candidates
    
    locate_nb()
    print(os.getcwd())
    

    【讨论】:

    • 好的,所以它会遍历每条路径,直到它与文件名匹配,谢谢
    【解决方案2】:

    Google colab 允许您将笔记本保存到 google drive,当您创建新笔记本时,您可以单击文件菜单中的“Locate in drive”访问此位置。

    请注意,您可以将驱动器安装到 colab 实例,这意味着您可以将 google 驱动器作为 colab 实例中的子文件夹访问,但是,如果您要在 本地查找物理位置google drive 文件夹(由 google drive 应用创建),那么这应该是这个位置(在 Mac 上)。

    /Volumes/GoogleDrive/My Drive/Colab Notebooks/
    

    但是,如果您已在 colab 上安装了 google 驱动器,并且正在通过 google colab 查找笔记本的保存位置,请试试这个 -

    path = '/content/drive/MyDrive/Colab Notebooks'
    

    【讨论】:

    • 对不起,我的问题并不清楚:我正在寻找的是 python 代码,它将返回 COLAB 笔记本的位置,无论它存储在驱动器的哪个位置。这样我就可以从那里导航到子文件夹。 (例如数据文件夹)
    【解决方案3】:

    由于 GoogleDrive 是您当前的工作目录,因此您需要先从 colab 挂载它:

    from google.colab import drive
    drive.mount('/content/drive/')
    

    那么您需要将您的目录更改为您的.ipynb 存储位置:

    import os 
    os.chdir('/content/drive/MyDrive/path/to/your/folder')
    

    最后,如果你这样做了

    import os 
    cwd= os.getcwd()
    print (cwd)
    

    您将获得类似/content/drive/MyDrive/path/to/your/folder 的内容,其中存储了您的.ipynb

    当你这样做时

    import subprocess
    subprocess.check_output(['ls'])
    

    您会在此处看到您的笔记本文件。

    如果您想导航到子文件夹,只需使用os.chdir()

    【讨论】:

    • 所以没有办法找到路径:'/content/drive/MyDrive/path/to/your/folder'?类似“获取当前笔记本的路径”的命令。
    • 我认为os.getcwd() 确实在做,对吧?
    • 是的,但是当您安装驱动器并运行 os.getcwd() 时,无论您的笔记本在 Google 驱动器中的哪个文件夹开始,您都会得到“/content/”。我想运行一个 nobeook 并找出它存储在谷歌驱动器的哪个子文件夹中 - 这样我就可以相对于该文件夹进行导航,这样如果我移动笔记本所在的文件夹集,一切仍然有效
    猜你喜欢
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2020-06-26
    • 2021-09-30
    • 2014-03-25
    • 2023-03-20
    相关资源
    最近更新 更多