How do I get the full path of the current file's directory?

前端 未结 15 1089
傲寒
傲寒 2020-11-22 17:01

I want to get the current file\'s directory path. I tried:

>>> os.path.abspath(__file__)
\'C:\\\\python27\\\\test.py\'

But how can

15条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 17:38

    IPython has a magic command %pwd to get the present working directory. It can be used in following way:

    from IPython.terminal.embed import InteractiveShellEmbed
    
    ip_shell = InteractiveShellEmbed()
    
    present_working_directory = ip_shell.magic("%pwd")
    

    On IPython Jupyter Notebook %pwd can be used directly as following:

    present_working_directory = %pwd
    

提交回复
热议问题