Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
Last active March 18, 2020 19:11
Show Gist options
  • Save blaylockbk/0590d05b0013500d0e3b38b9a523c025 to your computer and use it in GitHub Desktop.
Save blaylockbk/0590d05b0013500d0e3b38b9a523c025 to your computer and use it in GitHub Desktop.
For a file name that begins with '~' or an environment varialbe, update the file name with the environment home directory.
#==========================================================================
## Simple method, just deal with the '~' notation for home
#==========================================================================
import os
FILE = "~/path/to/file.data"
FILE = FILE.replace('~', os.environ['HOME'])
#==========================================================================
#==========================================================================
## More involved, but deals with other environment variables
#==========================================================================
import os
FILE = "$HOME/$CENTER/path/to/file.data"
path = []
for i in FILE.split('/'):
if i == '~':
path.append(os.environ['HOME'])
elif len(i) > 1 and i[0] == '$':
path.append(os.environ[i[1:]])
else:
path.append(i)
FILE = '/'.join(path)
#==========================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment