Python os.path.expandvars() Method

Python os.path.expandvars() Method

  1. Syntax of os.path.expandvars() Method
  2. Example 1: Use the os.path.expandvars() Method in Python
  3. Example 2: Use the os.path.expandvars() Method on Windows OS
  4. Example 3: Use an Unspecified Environment Variable in the os.path.expandvars() Method

Python os.path.expandvars() method is an efficient way of expanding the environment variables in a specified path. All the substrings of the form $name or ${name} are replaced with the set value of the environment variable name.

Syntax of os.path.expandvars() Method

os.path.expandvars(path)

Parameters

path - an address object of a file system path or a symlink. The object can either be an str or bytes.

Return

The return type of this method is a string value representing the new environment variable after expanding it, based on the input parameter.

Example 1: Use the os.path.expandvars() Method in Python

import os.path

path_address = "$HOME/File.txt"

new_path = os.path.expandvars(path_address)

print(new_path)

os.environ["HOME"] = "/home/Documents"

path_address = "$HOME/User/File.txt"

new_path = os.path.expandvars(path_address)

print(new_path)

os.environ["USER"] = "programmer"

path_address = "/home/${USER}/File.txt"

new_path = os.path.expandvars(path_address)

print(new_path)

Output:

//File.txt
/home/Documents/User/File.txt
/home/programmer/File.txt

In the code above, we have replaced the values of the environment variable HOME and USER with corresponding values.

Example 2: Use the os.path.expandvars() Method on Windows OS

import os.path

path_address_1 = R"% HOME %\Documents\File.txt"


path_address_2 = R"C:\User\$USERNAME\HOME\File.txt"

path_address_3 = R"${TEMP}\File.txt"

new_path1 = os.path.expandvars(path_address_1)

new_path2 = os.path.expandvars(path_address_2)

new_path3 = os.path.expandvars(path_address_3)

print(new_path1)

print(new_path2)

print(new_path3)

Output:

% HOME %\Documents\File.txt
C:\User\$USERNAME\HOME\File.txt
${TEMP}\File.txt

In this code, we have replaced the values of the environment variable HOME, USERNAME, and TEMP with corresponding values.

Example 3: Use an Unspecified Environment Variable in the os.path.expandvars() Method

import os.path

path_address = R"${MYHOME}/Documents / File.txt"

new_path = os.path.expandvars(path_address)

print(new_path)

Output:

${MYHOME}/Documents / File.txt

No environment variable is defined in the above code, so the path is unchanged and displayed as-is.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method