Python 101 – Expanding Paths and Variables

Working with file and folder paths in Python can take time and effort. Fortunately, the Python programming language has a couple of great packages to help you!

This tutorial will teach you how to expand an initial path component, such as a tilde (~). You will also learn how to expand/replace environment variables such as $USERNAME or $TEMPDIR.

You will learn how to do this using the following modules (if applicable):

  • os.path
  • pathlib

Let’s get started!

 

Expanding the User with os.path

You’ll need to write three simple lines of code to get started.

Take a look:

>>> import os
>>> os.path.expanduser ("~/test.log")
'/Users/mike/test.log'

Here you take a path that beings with a tilde and expand it using os.path.expanduser().

Expanding the User with pathlib

Now let’s find out how to use pathlib to expand the user instead of os.path!

Here’s the code you’ll need:

>>> from pathlib import Path
>>> p = Path("~/test.log")
>>> p.expanduser()
PosixPath('/Users/mike/test.log')

The primary difference here is that you are creating a Path() object. Then after you create that object, you can call the expanduser() method to do the magic!

The pathlib module has lots of other great methods. For example, if all you want is the current user’s home folder, you can call the home() method, like this:

>>> p.home()
PosixPath('/Users/mike')

Now you’re ready to move on and learn how to expand environment variables!

Expanding Environment Variables with os.path

Each operating system will have its environment variables that you can use. You may want to check what’s available by accessing os.environ.

Here’s an example from the author’s laptop:

>>> import os
>>> os.environ
environ({'SECURITYSESSIONID': '186a4', 'USER': 'mike', '__CFBundleIdentifier': 'com.wingware.wingpro', 'COMMAND_MODE': 'unix2003', 'PATH': '/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin', 'SHELL': '/bin/zsh', 'HOME': '/Users/mike', '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0', 'LaunchInstanceID': '4731D42A-3EA8-45E8-9921-EA9E897160E4', 'XPC_SERVICE_NAME': 'application.com.wingware.wingpro.446397.446405', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.1ldniyJtTK/Listeners', 'XPC_FLAGS': '0x0', 'LOGNAME': 'mike', 'TMPDIR': '/var/folders/2q/yhh5dc6n367gftpg043m0x3r0000gn/T/', 'LC_CTYPE': 'UTF-8', 'WING_ORIG_LANGUAGE': '__undefined__', 'LANGUAGE': 'en', 'SHLVL': '0', 'PWD': '/Users/mike', 'OLDPWD': '/', 'HOMEBREW_PREFIX': '/opt/homebrew', 'HOMEBREW_CELLAR': '/opt/homebrew/Cellar', 'HOMEBREW_REPOSITORY': '/opt/homebrew', 'MANPATH': '/opt/homebrew/share/man::', 'INFOPATH': '/opt/homebrew/share/info:', '_': '/Applications/Wing Pro.app/Contents/Resources/bin/__os__/macos/printenv', 'WINGDB_SPAWNCOOKIE': 'LmdeVvJlrPMSBCZu', 'PYTHONIOENCODING': 'utf_8', 'WINGDB_PARENT_PIDS': '699', 'WINGDB_ACTIVE': '699'})

With that in mind, let’s try expanding a couple of the variables mentioned above:

>>> os.path.expandvars("$TMPDIR")
'/var/folders/2q/yhh5dc6n367gftpg043m0x3r0000gn/T/'
>>> os.path.expandvars("$PATH")
'/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin'

Now give it a try on your computer. If it doesn’t work, check your copy of os.environ to ensure you use the correct names.

Unfortunately, pathlib does not support expanding environment variables in any way.

Wrapping Up

If you need to work with file or folder paths, check out os.path and pathlib. Most developers who use Python 3 prefer pathlib as it’s much newer and makes some things easier. However, it is good to know about both because you will encounter os.path in legacy code.