Perl-Python-a-Day: split a file full path

Xah Lee xah at
Mon Oct 17 04:00:58 CEST 2005

Split File Fullpath Into Parts

Xah Lee, 20051016

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. For example:


'/Users/t/web/perl-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. Example:

# python
import os.path

myPath = '/Users/t/web/perl-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

The official doc of the os.path module is at:

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

# perl
use File::Basename;

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

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

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

Note: the second argument to fileparse() is a list of regex. In
particular, you need to escape the dot.

For the official doc, type in command line: “perldoc File::Path”.
This post is archived at

Schemers, a scsh version will be appreciated.

 xah at xahlee.org

More information about the Python-list mailing list