Python, Perl: Split File Path into Parts

By Xah Lee. Date: . Last updated: .

Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core part and a extension part.



'/Users/t/web/python/'  (directory name)
'I_Love_You'                 (file's base name)
'.html'                      (file's “extension”)

Depending on the language, some language will remove the trailing slash after the dir name, and some will omit the dot before the suffix.


In Python, to split a full path into parts is done with the os.path module.

# -*- coding: utf-8 -*-
# python 2

import os.path

myPath = '/Users/t/web/python/I_Love_You.html'
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)

print dirName   # /Users/t/web/perl-python
print fileName  # I_Love_You.html
print fileBaseName    # I_Love_You
print fileExtension   # .html


In Perl, splitting a full path into parts is done like this:

# -*- coding: utf-8 -*-
# perl

use File::Basename;

$myPath = '/Users/t/web/python/I_Love_You.html';

($fileBaseName, $dirName, $fileExtension) = fileparse($myPath, ('\.html') );

print $fileBaseName, "\n";   # I_Love_You
print $dirName, "\n";  # /Users/t/web/python/
print $fileExtension, "\n";  # .html

Note: the second argument to fileparse() is a list of regex. In particular, you need to escape the dot. Normally, one gives it a value such as ('\.html', '\.HTML', '\.jpg', '\.JPG'). Yes, it is case sensitive. If you want to match any extension (that is, the string after the last dot), use ('\.[^.]+$').

For the official doc, type in command line: “perldoc File::Basename” or