__file__ does not exist in Jupyter Notebook

前端 未结 4 2139
无人及你
无人及你 2020-12-23 18:54

I\'m on a Jupyter Notebook server (v4.2.2) with Python 3.4.2 and I want to use the global name __file__, because the notebook will be cloned from other users an

4条回答
  •  情书的邮戳
    2020-12-23 19:14

    __file__ might not be available for you, but you can get current folder in which your notebook is located in different way, actually.

    There are traces in global variables, if you will call globals() you will see that there is an element with the key _dh, that might help you. Here how I managed to load the data.csv file that is located in the same folder as my notebook:

    import os
    
    current_folder = globals()['_dh'][0]
    
    # Calculating path to the input data
    data_location = os.path.join(current_folder,'data.csv')
    

提交回复
热议问题