Last active
March 18, 2020 19:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#========================================================================== | |
## 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