February 15, 2022    Share on: Twitter | Facebook | HackerNews | Reddit

Python - get file and path parts (dirname, base name, extension)

To split full file name (e.g. /root/dir/sub/file.ext) with path into various parts you can use python build-in functions from the os module. You can retrieve: - base name - just file name with extension (in our example: file.ext) - file path (in our example: /root/dir/sub/) - core name from the file name (in our example: file) - extension from the file name (in our example: .ext)

See the code below:

    import os
    full_file = '/root/dir/sub/file.ext'

    # file name with extension 
    basename = os.path.basename(full_file)
    print(f'basename: {basename}')

    # full path to file
    dirname = os.path.dirname(full_file)
    print(f'dirname: {dirname}')

    # extract core name and extension from the filename
    core_name, ext = os.path.splitext(basename)
    print(f'core_name: {core_name}', f'ext: {ext}')

Example results:

>>> import os
>>> base=os.path.basename('/root/dir/sub/file.ext')
>>> base
'file.ext'
>>> os.path.splitext(base)
('file', '.ext')
>>> os.path.splitext(base)[0]
'file'

Reference: