os.path.commonprefix() in Python is used to get the longest common path prefix in a list of paths. This method only returns the value of the common prefix in the specified list, the return value may or may not be a valid path as it checks for the common prefix by comparing character by character in the list.
For example, consider the following list of paths:
list of paths common prefix [`/ home / User / Photos`, / home / User / Videos`] / home / User / A valid path [`/ usr / local / bin`,` / usr / lib`] / usr / l Not a valid path
Syntax: os.path.commonprefix (list)
path : A list of path-like object. 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 longest common path prefix in specified list.
Code # 1: Using the os.path.commonprefix method ()
Longest common path prefix: / home / User / D Longest common path prefix: / usr /
Note. If the specified list is empty,
os.path.commonprefix ( ) will return an empty string.
As information protection continues to be a growing concern for businesses today, IT security certifications have become highly desirable, even as the number of certifications has grown. Now you can s...
We live in an age of so-called Big Data. We hear terms like data scientist, and there is much talk about analytics and the mining of large amounts of corporate data for tidbits of business value. Ther...
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...
Mark Lutz is the global leader in Python training, author of the oldest and best-selling Python texts, and a pioneer in the Python community since 1992.
Mark Lutz is the author of the found...