The Python os.path.basename method is used to extract the base name of a pathname. This is useful when you want to process only the filename or directory name from a full pathname.
Table of Contents
Understanding os.path.basename method
The os.path.basename() method in Python, part of the os.path module, extracts the filename from a path string. It returns the final component of the path, which represents the file or directory name, excluding the directory path.
This method internally use os.path.split() method to split the specified path into a pair (head, tail). os.path.basename() method returns the tail part after splitting the specified path into (head, tail) pair.
os.path.basename Syntax
Syntax: os.path.basename(path)
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!Parameter:
- path: A path-like object representing a file system path.
- Return Type: This method returns a string value which represents the base name the specified path.
Examples of os.path.basename method in Python
Here are three more examples of the os.path.basename method in action:
>>> os.path.basename('C/foo/bar/baz.txt')
'baz.txt'
>>> os.path.basename('/etc/passwd')
'passwd'
>>> os.path.basename('/etc/telegraf/telegraf.conf')
'telegraf.conf'
>> os.path.basename('/etc/ssh/ssh_config.d')
'ssh_config.d'
>>>os.path.basename('/etc/ssh/ssh_config.d/')
''
Can os.path.basename() handle both file paths and directory paths?
Yes, it can handle both file paths and directory paths. It returns the final component, which could be either a file name or the last directory in the path.
What happens if the path includes a trailing slash?
If the path ends with a slash, indicating a directory, os.path.basename() returns an empty string.
Python os.path.dirname vs Python os.path.basename
os.path.dirname is the inverse of os.path.basename – it returns the directory name portion of a pathname. For example,
>> os.path.dirname('/etc/telegraf/telegraf.conf')
'/etc/telegraf'
Are there any other methods related to os.path.basename that I should know about?
There are several other useful methods related to pathnames that you should be aware of:
- os.path.split() splits a full pathname into its individual filename and directory name components
- os.path.join() combines multiple pathname components
For example:
>>> os.path.split('/etc/telegraf/telegraf.conf')
('/etc/telegraf', 'telegraf.conf')
>>> print(os.path.join("/etc", "telegraf","telegraf.conf"))
/etc/telegraf/telegraf.conf
understanding os.path in Python
The os.path module in Python provides functionalities for common pathname manipulations, focusing on path-related operations rather than file input and output. It allows you to interact with file paths in a platform-independent manner, ensuring compatibility across different operating systems.
- os.path.join(path1, path2, …): Joins one or more path components intelligently.
- os.path.exists(path): Checks if the path exists.
- os.path.basename(path): Returns the final component of a path.
- os.path.dirname(path): Returns the directory name from a path.
- os.path.splitext(path): Splits the extension from a path.
- os.path.abspath(path): Returns the absolute path of a file or directory.
- os.path.isfile(path): Checks if the path points to a file.
- os.path.isdir(path): Checks if the path points to a directory.
understanding sys.path in Python
understanding os.path.join in Python
3 effective Ways to Get file path in Linux