Skip to content

Instantly share code, notes, and snippets.

@luochen1990
Created November 11, 2021 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luochen1990/9b1ffa30f5c4a721dab5991e040e3eb1 to your computer and use it in GitHub Desktop.
Save luochen1990/9b1ffa30f5c4a721dab5991e040e3eb1 to your computer and use it in GitHub Desktop.
Path relative to current file
#!/usr/bin/env python3
import sys
import os
relpath = lambda p: os.path.normpath(os.path.join(os.path.dirname(__file__), p))
sys.path.append(relpath('..')) # so that you can import from upper dir
path1 = relpath('../src/main.py')
print(f'path1 relative to cwd: {path1}')
print(f'path1 absolute path: ({ os.path.abspath(path1) })')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment