Skip to content

Instantly share code, notes, and snippets.

@travis23
Last active January 21, 2019 20:55
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 travis23/a6fa556764f2b93e8ed5d6583935774e to your computer and use it in GitHub Desktop.
Save travis23/a6fa556764f2b93e8ed5d6583935774e to your computer and use it in GitHub Desktop.
[universal directory] ways to find home directory and other common directories on different machines and operating systems #python #directory #os
import os
import pathlib
# 1
# -------------------------------------------------
os.environ["ALLUSERSPROFILE"]
# The environment variable "ALLUSERSPROFILE" is available on Windows only.
# On Windows returns
# 'C:\\ProgramData'
# 2
# --------------------------------------------------
os.path.expanduser('~\')
# This apparently works accross operating systems.
# On Windows this returns 'C:\\Users\\[User]/'
# 3
# -------------------------------------------------
str(pathlib.Path.home())
# The apparently works accross operating systems. It does rely on environment variables internally.
# API at https://docs.python.org/3.6/library/pathlib.html#basic-use
# I like this
# On Windows this returns
# 'C:/Users/[User]'
# Also see [How to find the real user home directory using python? - Stack Overflow](https://stackoverflow.com/questions/2668909/how-to-find-the-real-user-home-directory-using-python)
# 4
# ---------------------------------------------------------------------
def _get_reporoot():
"""Returns the absolute path to the repo root directory on the current
system.
"""
import package_name
global reporoot
if reporoot is None:
medpath = path.abspath(gmf_models.__file__)
reporoot = path.dirname(path.dirname(medpath))
return reporoot
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment