os.path.split() method - Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report os.path.split() method in Python is used to split a file path into two parts:Head: Everything leading up to the last path component (typically the directory path).Tail: The final component of the path (typically the file or folder name).For example, consider the following path name:path name = '/home/User/Desktop/file.txt'For the above example:Head = /home/User/Desktop/Tail = file.txtKey BehaviorIf the path ends with a slash (/), the tail is an empty string.If there is no slash in the path, the head is an empty string.The returned value is always a tuple: (head, tail).For example:PathHeadTail'/home/user/Desktop/file.txt' '/home/user/Desktop/' 'file.txt''/home/user/Desktop/file.txt''/home/user/Desktop/' {empty}'file.txt' {empty}'file.txt'Syntax of os.path.split() Methodos.path.split(path)Parameter: A path-like object representing a file system path. A path-like object is either a str or bytes object representing a path.Return Type: This method returns a tuple that represents head and tail of the specified path name.Examples os.path.split() MethodExample 1: Splitting Different File PathsIn this example, the code demonstrates the use of os.path.split() method to split a given file path into its directory (head) and file/directory name (tail). It prints the head and tail for three different path examples: '/home/User/Desktop/file.txt', '/home/User/Desktop/', and 'file.txt'. Python import os path = '/home/User/Desktop/file.txt' ht = os.path.split(path) print("Head:", ht[0]) print("Tail:", ht[1], "\n") path = '/home/User/Desktop/' ht = os.path.split(path) print("Head:", ht[0]) print("Tail:", ht[1], "\n") path = 'file.txt' ht = os.path.split(path) print("Head:", ht[0]) print("Tail:", ht[1]) OutputHead: /home/User/Desktop Tail: file.txt Head: /home/User/Desktop Tail: Head: Tail: file.txt Explanation:first example separates the file from its full directory.second path ends with a slash, so the tail is empty.third is just a filename without any directory, so the head is empty.Example 2: Handling an Empty PathThis example demonstrates the use of the os.path.split() method with an empty path. It shows that when an empty path is provided, the os.path.split() function returns empty strings for both the head and tail, as indicated in the comments. Python import os path = '' ht = os.path.split(path) print("Head:", ht[0]) print("Tail:", ht[1]) OutputHead: Tail: Explanation: Providing an empty string as the path returns empty strings for both head and tail.Related articles: os module Comment More infoAdvertise with us Next Article os.path.isdir() method - Python I ihritik Follow Improve Article Tags : Python python-os-module Practice Tags : python Similar Reads Python | os.path.isabs() 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.isfile() method The os.path.isfile() method in Python is used to check if a path refers to a regular file (not a directory or any other type of file system object). This method can be particularly useful when you need to validate file paths before performing file operations like reading, writing, or modifying files 2 min read os.path.isdir() method - Python os.path.isdir() method in Python checks whether the specified path is a directory or not. This method follows the symbolic link, which means if the specified path is a symbolic link pointing to an existing directory then the method will return True.Example: Using os.path.isdir() methodThis code demo 2 min read Python | os.path.islink() 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.ismount() method os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ism 2 min read Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport 4 min read Python | os.path.normcase() 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.normpath() 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.realpath() 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.relpath() 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 3 min read Like