Skip to content

Instantly share code, notes, and snippets.

@kathawala
Created December 22, 2014 09:49
Show Gist options
  • Save kathawala/62562ca8f332017a5f5c to your computer and use it in GitHub Desktop.
Save kathawala/62562ca8f332017a5f5c to your computer and use it in GitHub Desktop.
Python script getting its own directory
#!/bin/env python
import inspect
import os
import sys
def get_script_dir(follow_symlinks=True):
if getattr(sys, 'frozen', False): # py2exe, PyInstaller, cx_Freeze
path = os.path.abspath(sys.executable)
else:
path = inspect.getabsfile(get_script_dir)
if follow_symlinks:
path = os.path.realpath(path)
return os.path.dirname(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment