Python Expanduser
- Python's OS module offers tools for communicating with an operating system. OS is included in the basic utility modules for Python.
- This module offers a portable method of using functionality that is dependent on the operating system. Python's os.path module, a submodule of the OS module, is used to manipulate common pathnames.
- The Python function os.path.expanduser() is used to expand the path component (tilde symbol) or user in the specified path to the user's home directory.
- On Unix platforms, if the HOME environment variable is set, its value replaces the initial. Otherwise, the os.path.expanduser() method uses the built-in module pwd to search for the user's home directory in the password directory. The password directory is searched for any path that contains an initial "user" component.
- A defined path can be expanded relative to the home directory by using the os.path.expanduser() method in Python. The desired route can be extended using the tilde symbol, with user as the default.
- On the Windows platform, if the HOME, as well as USERPROFILE environment variables, are set, their values are substituted for the initial. If not, the environment variables HOMEPATH and HOME DRIVE will be used. While paths with starting "user" components are handled by substituting "user" from the path generated above for the last directory component.
Syntax:
Syntax: os.path.expanduser(path)
Parameters:
Path: A object that resembles a path and represents a file system path. A string or an object made up of bytes that represents a path is a path-like object.
Return Type: After extending an initial path element or user in the given path, this function compares a string value that reflects the path.
Earlier version
- To extend the original path of such a component or user in the specified path to the user's home directory, use Python's os.path.expanduser() method.
- If installed, the value of an environment variable HOME is used in place of the leading character on Unix platforms. Otherwise, use the built-in PWD module with the os.path.expanduser() search method to locate the user's home directory within the password directory. In the password directory, a direct lookup is performed to find the path that leads to the original custom component.
- The HOME environment variable value and USERPROFILE, if set, are substituted for the beginning character on the Windows platform. If not, the environment variables HOMEPATH and HOME DRIVE will be used. While the route containing the initial "user" component is processed by substituting "user" from the above path for the last directory component.
Program 1
# os.path.expanduser() method explanation in Python program. # importing the module os.path import os.path # Path path = "~/file.txt" # Extend a first component # in the indicated path #os.path.expanduser() function is being used. full_path = os.path.expanduser(path) # After, print the path. # Increasing the first component # in the indicated path print(full_path) # Modify the value of # HOME environmental parameter os.environ["HOME"] = "/home / jtp" #Expand the initial component now. #in the same direction #os.path.expanduser() function is being used. full_path = os.path.expanduser(path) # After, print the path. # Increasing the first component # in the indicated path print(full_path) # While expanding, a preliminary # The user component is examined # directly in the directory for passwords. # Path including a first # User component. path = "~isravani / file.txt" #Expand the initial component now. #component in the same direction #os.path.expanduser() function is being used. full_path = os.path.expanduser(path) # after,print the path # the initial ~user expanding # The user component is examined print(full_path)
output:
C:\Users\20211/file.txt C:\Users\20211/file.txt C:\Users\isravani / file.txt
Program 2:
#Python application demonstrating the os.path.expandvars() technique. # importing os.path module import os.path # Expanding names on Windows # are supported as well# # $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" # Make the environment variables larger # with their associated # in the specified pathways exp_var1 = os.path.expandvars(path1) exp_var2 = os.path.expandvars(path2) exp_var3 = os.path.expandvars(path3) # Print the paths specified with #Explicit number of environment variables print(exp_var1) print(exp_var2) print(exp_var3) # In the preceding instance #Method os.path.expandvars() # substituted for the environment variables # "USERNAME," "HOMEPATH," and "TEMP" # with their associated values
Output:
% HOMEPATH %\Directory\file.txt C:\Users\20211\Directory\file.txt C:\Users\20211\AppData\Local\Temp\file.txt
Program 3
#Python application demonstrating the os.path.expandvars() technique. # importing os.path module import os.path #If environment variables, then # is incorrect or nonexistent. # the provided path will be. % left unaltered # Path path = R"${MYHOME}/Directory / file.txt" # Make the environment variables larger # with their associated # in the specified pathways exp_var = os.path.expandvars(path) # Use to print the supplied path #Environment variables listed in number print(exp_var) # As the environment variable "MYHOME" # is not present, so #Method os.path.expandvars() # returns the specified path #No change
Output:
${MYHOME}/Directory/file.txt