如何为Microsoft Azure Jupyter笔记本指定路径?

时间:2019-08-25 23:35:34

标签: python azure jupyter-notebook

我正在尝试将为Jupyter Notebook编写的Python脚本上载到Microsoft Azure Notebooks,但无法正常工作。

Microsoft Azure项目有一个包含我要读取的XLSX文件和Python脚本的文件夹。当我在Jupyter Notebook上运行Python脚本时,它无法访问我先前上传到项目中的文件夹。

path =r'/Users/chorozco/Documents/Dr.Irwin/6 Column Data Acquisition Converted'
filenames = glob.glob(path + "/*.xlsx")

path = os.getcwd()
files = os.listdir(path)
files

我期望“ 6列数据转换已转换”中的文件列表,但是我却获得了Microsoft Azure Project中的文件列表:

期望: Script run from Anaconda in my computer

现实: Script run from Microsoft Azure Notebooks

2 个答案:

答案 0 :(得分:0)

仅根据您的代码path =r'/Users/chorozco/Documents/Dr.Irwin/6 Column Data Acquisition Converted',我认为您使用的是MacOS,而不是Linux。但是,Microsoft Azure笔记本基于Linux来运行。因此,Azure Notebook和您的本地Anaconda之间的代码os.listdir(path)之所以不同,是由于在Linux和Windows以外的其他OS(例如MacOS)上针对此功能os.listdir的Python实现不同而导致的,结果是没有文件递归在目录中列出。

这是我的步骤作为参考。

  1. 我在Microsoft Azure笔记本帐户中创建了一个名为Test for listing xlsx files in a path of Azure Notebook Project的新项目,并创建了一个新文件夹6 Column Data Acquisition Converted,如下图所示。

    图1。

    enter image description here

    图2。

    enter image description here

  2. 我在本地Windows上创建了一些xlsx文件以进行上传,然后移至我先前在Azure Notebook项目中创建的文件夹以进行上传操作,如下图所示。

    图3。

    enter image description here

    图4。

    enter image description here

    图5。

    enter image description here

    图6。

    enter image description here

  3. 我在Python 3.6中创建了一个笔记本,以与您的笔记本运行相同的代码。

    图7。

    enter image description here

    图8。

    enter image description here

  4. 我发现结果与本地Linux和Windows上的代码相同,但与MacOS上的结果不同。

    图9。几乎相同的代码在Azure Notebook的Python 3.6中运行

    enter image description here

    图10。几乎相同的代码在Azure Notebook的Python 2.7中运行

    enter image description here

    图11. Windows上os.listdir的结果

    enter image description here

    图12. Linux上os.listdir的结果

    enter image description here

因此,我认为该问题是由MacPython引起的,请再次检查。希望对您有所帮助。

答案 1 :(得分:0)

哇,非常感谢Peter的解释。这非常有帮助。我刚刚开始使用Python进行编码,我什至不知道尝试在Mac上实现Python会有什么区别。