Python gives varied modules and strategies for working with the recordsdata and their paths. For example, extracting the listing identify from a path, getting the file identify from a path, or just splitting a path into two components. In such conditions, the “os.path.cut up()” methodology is utilized in Python. This methodology takes the trail and retrieves a tuple containing the top and tail of the trail.
This write-up presents a complete information on Python’s “os.path.cut up()” methodology utilizing quite a few examples and overlaying the under content material:
What’s the “os.path.cut up()” Technique in Python?
In Python, the “os.path.cut up()” methodology is utilized to separate/separate a given path identify into teams of “head” and “tail”. The top is the listing identify as much as the final path separator, and the “tail” is the file identify or an empty string if there isn’t any path separator.
Syntax
Parameters
Within the above syntax, the “path” parameter is an “str/string” or “bytes” path-like object equivalent to a path on the file system.
Return Worth
The “os.path.cut up()” methodology returns a tuple of two strings, the primary string being the “head” and the second string being the “tail”. If the trail doesn’t include any path separators, then the top might be empty, and the tail would be the similar as the trail.
Instance 1: Making use of the “os.path.cut up()” Technique to Cut up the Specified Path
The under code is applied to separate the trail into head and tail pair:
path = r‘C:UserspDocumentsprogramfilename.txt’
print(os.path.cut up(path), ‘n’)
path =r‘C:UserspDocumentsprogram’
print(os.path.cut up(path), ‘n’)
path = ‘filename.txt’
print(os.path.cut up(path), ‘n’)
Within the above code:
-
- The “os” module is imported.
- The “os.path.cut up()” methodology takes the whole path, together with the file identify retrieving the tuple with the top and tail of the trail.
- The “os.path.cut up()” methodology is used once more twice with the required path with out the filename and with the filename, respectively to retrieve the top and tail within the type of a tuple.
Output
The top and tail pair of the required paths have been returned by the “os.path.cut up()” methodology, accordingly.
Instance 2: Making use of the “os.path.cut up()” Technique to Cut up the Empty Path
This code is used to separate the empty path:
import os
path = ”
print(os.path.cut up(path))
Within the above code, the “os.path.cut up()” methodology takes an “empty path” as an argument and retrieves a tuple containing the top and tail of the trail.
Output
The empty path is cut up into an empty head and tail.
Conclusion
The “os.path.cut up()” methodology of the “os” module is used to separate/separate a specified path and return a tuple containing the “head” and “tail” of the trail. This methodology takes the empty path and returns an empty tuple with none head and tail path worth. This publish offered an in depth tutorial of Python’s “os.path.cut up()” methodology utilizing quite a few examples.