Search code examples
pythonpathdirectorytildegetcwd

Python os.getcwd() returns with tilde in the path. e.g. C:\MYFOLD~1\test


How can I get python to return the full pathname of C:\myfolderisafolder\test?


Solution

  • E:\dev>cd VARESE~1
    
    E:\dev\VARESE~1>python
    >>> import os
    >>> os.getcwd()
    'E:\\dev\\VARESE~1'
    >>> exit()
    
    E:\dev\VARESE~1>cd ..
    E:\dev>cd VAResearchDemo
    
    E:\dev\VAResearchDemo>python
    >>> import os
    >>> os.getcwd()
    'E:\\dev\\VAResearchDemo'
    >>> exit()
    

    As you can see, if I run python in VARESE~1 directory, os.getcwd() returns short path. If I run python in same directory but with long path, it returns long path.

    So, you should try to run python in C:\myfolderisafolder\test (check link's properties or how you run it).

    But if you need to convert a short path to a long path, you have to call win32's GetLongPathName function