Skip to content

Instantly share code, notes, and snippets.

@aelaguiz
Created January 12, 2014 15:28
Show Gist options
  • Save aelaguiz/8386014 to your computer and use it in GitHub Desktop.
Save aelaguiz/8386014 to your computer and use it in GitHub Desktop.
weird behavior of relpath
Python:
os.chdir(path)
cwd = os.getcwd()
print path
print cwd
print os.path.relpath(path, start=cwd)
output:
./themes/bold
/Users/aelaguiz/workspace/cratejoy/themes/bold
themes/bold
From command line:
(venv)amir-laptop:cratejoy aelaguiz$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path
>>> os.path.relpath("./themes/bold", "/Users/aelaguiz/workspace/cratejoy/themes/bold")
'.'
@expl0ratory
Copy link

I think the fact it says it doesn't use the FS is kind of a lie, it's at least aware of cwd, so I think your os.chdir is confusing things.

$ python
Python 2.7.6 (default, Nov 29 2013, 07:30:55)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.relpath('./share/vim', '/Users/alex/share/vim')
'.'
>>> os.path.relpath('./share/vim', '/Users/alex/does/not/exist/share/vim')
'../../../../../share/vim'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment