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

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

+4
source share
4 answers
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 the python directory in VARESE~1 , os.getcwd() returns a short path. If I run python in the same directory but with a long path, it returns a long path.

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

But if you need to convert the short path to the long path, you need to call the win32 function GetLongPathName

+3
source

Try using os.path.realpath , os.path.normpath .

0
source

Perhaps this would help:

 fullpath = os.path.expanduser('~/my/path') 
0
source

You can simply split the line with .split () on the tilde and then rejoin the full path to the file using the .join () methods.

0
source

All Articles