In Python, what is the os.path.abspath() function used for?As a Python programmer, you may do a wide variety of OS-related tasks with the help of the language’s dedicated OS module. Quite a few useful features are already included, so there’s no need to set up any other modules.Path is a submodule of the OS module that has built-in functions for working with the names of files and folders located everywhere in the file system.Consider the following case:os.path.dirname(path)It is expected to return the path’s directory name that was sent to it as an input.The expected result of using os.path.basename(path) is to get the path’s base name.os.path.commonprefix(listOfPaths)It is expected that this method would return the prefix shared by all the routes in the supplied array. The result will be a null string if no common prefix is found.os.path.exists(path)There is a Boolean value that may be retrieved from this function. It will return true if the specified route already exists in our system, and false otherwise.This function, os.path.lexists(path), returns true if the given path has any missing or corrupted symbols.os.path.getatime(path)It returns a floating-point integer that reflects the amount of time in seconds before the file specified by the function’s path parameter is accessed. There will be an error message shown if the file does not already exist.If you use os.path.getmtime(path), the method will return a float that corresponds to the number of seconds since the file’s last modification. An error will be generated if the specified file does not exist.os.path.getsize(path)It provides the file’s size in bytes. The error occurs if the specified file does not exist.os.path.splitdrive(path)If we send it a whole route to this function, it will return a spliced version of that path. There are two pieces: the driving unit and the rear.Drive letters, such as C:, D:, etc., designate the hard drive(s) on which a certain filesystem (or path) resides.In a file’s path, the tail is the last component.os.path.samefile(path1,path2)If the two paths supplied to this method, path1 and path2, both point to the same file or directory, then the function should return true. It will return false if the paths specified by path1 and path2 refer to separate files or folders.This function, os.path.splitext(path), is meant to divide the specified path into its component parts—the root and the extension. Including the period itself, everything in the path name before the dot (.) is the root, and anything after it is an extension. There won’t be any content in the second section if an expansion isn’t included.os.path.abspath(path)With a given route, this method will return the pathname after normalisation. It gives back the full path to the file on the system.Syntax:
- os.path.abspath(path)
Its return value is the full, fully-qualified name of the route specified.For starters:
- # importing os.path module in the file
- import os.path
- # file name
- fileName = ‘test.py’
- #print the absolute path of the file
- print(os.path.abspath(fileName))
# file name
fileName = ‘test.py’
#print the absolute path of the file
print(os.path.abspath(fileName))
Outp ut:\sThis is why the above code tries to output the absolute path to the provided file. To begin, we used the import keyword to bring in the os.path module. Next, we grabbed the name of a file already in the working directory. The name of the file was sent as an input to the os.path.abspath() module, and then printed using the print command.Keep in mind that we may utilise the function after moving to a new working directory.The Second Illustration:
- #importing the os module to use the necessary functions
- import os
- # file name
- fileName = ‘./javatpoint/try.png’
- # change the current working directory
- os.chdir(“./javatpoint/testingFolder”)
- # prints the absolute path of current working directory with file name
- print(os.path.abspath(fileName))
# file name
fileName = ‘./javatpoint/try.png’
# change the current working directory
os.chdir(“./javatpoint/testingFolder”)
# prints the absolute path of current working directory with file name
print(os.path.abspath(fileName))
Outpu t:\simage@@@1With the aforementioned code, we may use the OS module’s built-in features. So we just have to use one variable to keep track of the file’s name. By using the chdir() method, which is predefined in Python’s OS module, we have successfully moved the current working directory. The absolute filename has been retrieved using the abspath() method. If the result is printed, we can see that the absolute route has been modified.