Python | os.path.expandvars() method - GeeksforGeeks

10 Nov, 2021
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path the module is a submodule of OS module in Python used for common pathname manipulation.
os.path.expandvars() method in Python is used to expand the environment variables in the given path. It replaces substrings of the form $name or ${name} in the given path with the value of environment variable name.
If the given path contains malformed environment variable names or environment variables that do not exist then os.path.expandvars() method will left it unchanged.
On Windows, %name% expansion is also supported in addition to $name and ${name}expansion.
Syntax: os.path.expandvars(path)
Parameter:path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a string which represents the parameter with environment variables expanded.
Code #1: Use of os.path.expandvars() method
# Python program to explain os.path.expandvars() method     # importing os.path module import os.path  # Pathpath = "$HOME/file.txt"  # Expand the environment variables# with their corresponding # value in the given path  exp_var = os.path.expandvars(path)    # Print the given path with# environment variables expandedprint(exp_var)    # Change the value of # Environment variable 'HOME'  os.environ["HOME"] = "/home/GeeksForGeeks"   # Pathpath = "$HOME/Documents/file.txt"  # Expand the environment variables# with their corresponding # value in the given path  exp_var = os.path.expandvars(path)  # Print the given path with# environment variables expandedprint(exp_var)    # Create an environment variableos.environ["USER"] = "ihritik"  # Pathpath = "/home/${USER}/file.txt"  # Expand the environment variables# with their corresponding # value in the given path  exp_var = os.path.expandvars(path)  # Print the given path with# environment variables expandedprint(exp_var)    # In the above example,# os.path.expandvars() method replaced # the environment variable 'HOME' and 'USER'# with their corresponding values
/home/ihritik/file.txt
/home/GeeksForGeeks/Documents/file.txt
/home/ihritik/file.txt

 Code #2: Use of os.path.expandvars() method (On Windows)
# Python program to explain os.path.expandvars() method     # importing os.path module import os.path  # On Windows % name % expansions# are supported in addition to# $name and ${name}  # Path 1path1 = R"% HOMEPATH %\Directory\file.txt"  # Path 2path2 = R"C:\Users\$USERNAME\Directory\file.txt"  # Path 3path3 = R"${TEMP}\file.txt"  # Expand the environment variables# with their corresponding # value in the given paths  exp_var1 = os.path.expandvars(path1)exp_var2 = os.path.expandvars(path2)exp_var3 = os.path.expandvars(path3)  # Print the given paths with# environment variables expandedprint(exp_var1)print(exp_var2)print(exp_var3)    # In the above example # os.path.expandvars() method# replaced the environment variables# 'HOMEPATH', 'USERNAME' and 'TEMP'# with their corresponding values
\\Users\\Hritik\\\Directory\\file.txt
C:\\Users\\Hritik\\\Directory\\file.txt
C:\\Users\\Hritik\\AppData\\Local\\Temp\\file.txt

 Code #3: If environment variable does not exists
# Python program to explain os.path.expandvars() method     # importing os.path module import os.path  # If the environment variable # is malformed or does not exist# then the given path will be# left unchanged  # Pathpath = R"${MYHOME}/Directory/file.txt"  # Expand the environment variables# with their corresponding # value in the given paths  exp_var = os.path.expandvars(path)  # Print the given path with# environment variables expandedprint(exp_var)    # As 'MYHOME' environment variable# does not exists so# os.path.expandvars() method# will return the given path# unchanged
${MYHOME}/Directory/file.txt

 Reference: https://docs.python.org/3/library/os.path.html
mayank5326
Akanksha_Rai
prachisoda1234
Python OS-path-module
python-os-module
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()