Python | os.path.expanduser() method Last Updated : 11 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 module is sub module of OS module in Python used for common pathname manipulation.os.path.expanduser() method in Python is used to expand an initial path component ~( tilde symbol) or ~user in the given path to user’s home directory. On Unix platforms, an initial ~ is replaced by the value of HOME environment variable, if it is set. Otherwise, os.path.expanduser() method search for user’s home directory in password directory using an in-built module pwd. Path containing an initial ~user component is looked up directly in the password directory.On Windows platform, an initial ~ is replaced by the value of HOME and USERPROFILE environment variable, if it is set. Otherwise, HOMEPATH and HOMEDRIVE environment variable will be used. While Path containing an initial ~user component is handled by replacing the last directory component with ~user from the path derived above. Syntax: os.path.expanduser(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 value which represents the path after expanding an initial path component ~ or ~user in the given path. Code #1: Use of os.path.expanduser() method (On Unix) Python3 # Python program to explain os.path.expanduser() method # importing os.path module import os.path # Path path = "~/file.txt" # Expand an initial ~ component # in the given path # using os.path.expanduser() method full_path = os.path.expanduser(path) # print the path after # expanding the initial ~ component # in the given path print(full_path) # Change the value of # HOME environment variable os.environ["HOME"] = "/home / GeeksForGeeks" # Now, Expand the initial ~ component # in the same path # using os.path.expanduser() method full_path = os.path.expanduser(path) # print the path after # expanding initial ~ component # in the given path print(full_path) # While expansion, An initial # ~user component is looked # up directly in the password directory. # Path having an initial # ~user component path = "~ihritik / file.txt" # Expand the initial ~user # component in the given path # using os.path.expanduser() method full_path = os.path.expanduser(path) # print the path after # expanding the initial ~user # component in the given path print(full_path) Output: /home/ihritik/file.txt /home/GeeksForGeeks/file.txt /home/ihritik/file.txt Code #2: Use of os.path.expanduser() method (On Windows) Python3 # 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 1 path1 = R"% HOMEPATH %\Directory\file.txt" # Path 2 path2 = R"C:\Users\$USERNAME\Directory\file.txt" # Path 3 path3 = 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 expanded print(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 Output: \\Users\\Hritik\\\Directory\\file.txt C:\\Users\\Hritik\\\Directory\\file.txt C:\\Users\\Hritik\\AppData\\Local\\Temp\\file.txt Code #3: 3: If environment variable does not exists Python3 # Python program to explain os.path.expandvars() method # importing os.path module import os.path # If environment variable # is malformed or does not exists # then the given path will be # left unchanged # Path path = 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 patha with # environment variables expanded print(exp_var) # As 'MYHOME' environment variable # does not exists so # os.path.expandvars() method # will return the given path # unchanged Output: ${MYHOME}/Directory/file.txt Reference: https://docs.python.org/3/library/os.path.html Comment More infoAdvertise with us Next Article Python | os.path.expandvars() method I ihritik Follow Improve Article Tags : Python python-os-module Python OS-path-module Practice Tags : python Similar Reads OS Path module in Python OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje 2 min read os.path.abspath() method - Python The os.path.abspath() method in Python's os module is used to get the full (absolute) path of a file or folder. It's useful when you're working with relative paths but need to know the exact location on your system. If you pass a relative path, it converts it to an absolute one and if the path is al 2 min read Python | os.path.basename() method 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 module is sub module of OS module in Python used for common path name man 4 min read Python | os.path.commonpath() method 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 module is submodule of OS module in Python used for common path name mani 2 min read Python | os.path.commonprefix() method 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 module is submodule of OS module in Python used for common path name mani 2 min read Python | os.path.dirname() method 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 module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.exists() method os.path.exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. os.path.exists() Syntax in PythonSyntax: os.path.exists(path) Parameter: path: A path-like object repres 2 min read Python | os.path.lexists() method 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 module is sub module of OS module in python used for common path name man 2 min read Python | os.path.expanduser() method 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 module is sub module of OS module in Python used for common pathname mani 4 min read Python | os.path.expandvars() method 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 3 min read Like