【发布时间】:2021-02-18 07:02:36
【问题描述】:
我在 Jupyter Notebook 中编写了一些代码,这些代码在我的 Windows PC 上本地运行。当它导入文件夹时,我使用 "\"。
但是,我刚刚将所有文件夹移至我的 google 驱动器,并使用 Colab 打开它。 现在目录路径中的文件夹用 "/" 分隔,因此报错。
无论我是在 PC 上本地运行还是在线运行,我如何导入 foder。
# added this bit so i can import and run the code in colab
import os
from google.colab import drive
drive.mount('/content/drive')
os.chdir('/content/drive/My Drive/Dashboarding_Project/Folder_with_all_IV_curves/Python_IV')
#From the JUPYTER NOTEBOOK file
import pandas as pd
import os
import re
#these variables contain hte excell file with hte mask info & the file with the batch info
meta_mask_name = "MASK-1_2020-03-01.xlsx"
meta_batch_name = "BATCH-1-2_2020-8-20--EXAMPLE.xlsx"
#This is the main directory of the foler structure
path_parent = os.path.dirname(os.getcwd())
print("Main folder: ", path_parent)
print("Folders: ", os.listdir(path_parent))
print (os.listdir(path_parent +r"\MASK_META")) # this gives error now that i am using it online.
输出:
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/Dashboarding_Project/Folder_with_all_IV_curves\\MASK_META'
上下文
我同时使用 Colab 和 Jupiter,因为:
- Colab 易于分享,无需下载任何内容,可以在任何地方访问。
- Jupyter 我可以在本地使用和部署PANEL,而 Colab 不允许我在本地查看它。
我的最终目标是:
- 完全在线的仪表板(面板或其他东西)
- 仪表板在不是我的 PC 的服务器上运行(如 Heroku)
- 我可以将链接发送给某人,他们可以查看。
也许有人可以解决这个问题,避免主要问题。
【问题讨论】:
-
在下面对我的答案进行了几次迭代。我现在还包含了一个关于如何使用
pathlib.Path解决特定代码可移植性问题的示例。
标签: python directory dashboard holoviews