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

前端 未结 15 948
傲寒
傲寒 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:28

    Try this:

    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    
    0 讨论(0)
  • 2020-11-22 17:33

    In Python 3.x I do:

    from pathlib import Path
    
    path = Path(__file__).parent.absolute()
    

    Explanation:

    • Path(__file__) is the path to the current file.
    • .parent gives you the directory the file is in.
    • .absolute() gives you the full absolute path to it.

    Using pathlib is the modern way to work with paths. If you need it as a string later for some reason, just do str(path).

    0 讨论(0)
  • 2020-11-22 17:33

    You can use os and os.path library easily as follows

    import os
    os.chdir(os.path.dirname(os.getcwd()))
    

    os.path.dirname returns upper directory from current one. It lets us change to an upper level without passing any file argument and without knowing absolute path.

    0 讨论(0)
  • 2020-11-22 17:33

    I have made a function to use when running python under IIS in CGI in order to get the current folder:

    import os 
       def getLocalFolder():
            path=str(os.path.dirname(os.path.abspath(__file__))).split('\\')
            return path[len(path)-1]
    
    0 讨论(0)
  • 2020-11-22 17:35

    USEFUL PATH PROPERTIES IN PYTHON:

     from pathlib import Path
    
        #Returns the path of the directory, where your script file is placed
        mypath = Path().absolute()
        print('Absolute path : {}'.format(mypath))
    
        #if you want to go to any other file inside the subdirectories of the directory path got from above method
        filePath = mypath/'data'/'fuel_econ.csv'
        print('File path : {}'.format(filePath))
    
        #To check if file present in that directory or Not
        isfileExist = filePath.exists()
        print('isfileExist : {}'.format(isfileExist))
    
        #To check if the path is a directory or a File
        isadirectory = filePath.is_dir()
        print('isadirectory : {}'.format(isadirectory))
    
        #To get the extension of the file
        fileExtension = mypath/'data'/'fuel_econ.csv'
        print('File extension : {}'.format(filePath.suffix))
    

    OUTPUT: ABSOLUTE PATH IS THE PATH WHERE YOUR PYTHON FILE IS PLACED

    Absolute path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2

    File path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2\data\fuel_econ.csv

    isfileExist : True

    isadirectory : False

    File extension : .csv

    0 讨论(0)
  • 2020-11-22 17:36

    System: MacOS

    Version: Python 3.6 w/ Anaconda

    import os
    
    rootpath = os.getcwd()
    
    os.chdir(rootpath)
    
    0 讨论(0)
提交回复
热议问题