Skip to content

Instantly share code, notes, and snippets.

@alecordev
Last active July 26, 2017 14:18
Show Gist options
  • Save alecordev/a0c74cd4770d2a07a39b887d384701aa to your computer and use it in GitHub Desktop.
Save alecordev/a0c74cd4770d2a07a39b887d384701aa to your computer and use it in GitHub Desktop.
Python path
os.chdir(path)
os.path.dirname(os.path.abspath(__file__))
os.path.dirname(os.path.realpath(__file__))
os.path.abspath(os.path.dirname(sys.argv[0]))
# Long
import os
print('Path at terminal when executing this file')
print(os.getcwd() + '\n')
print('This file path, relative to os.getcwd()')
print(__file__ + '\n')
print('This file full path (following symlinks)')
full_path = os.path.realpath(__file__)
print(full_path + '\n')
print('This file directory and name')
path, filename = os.path.split(full_path)
print(path + ' --> ' + filename + '\n')
print('This file directory only')
print(os.path.dirname(full_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment