Split File Fullpath Into Parts

Xah Lee, 2005-10-16

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/I_Love_You.html'
becomes 

'/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: http://www.python.org/doc/2.4.1/lib/module-os.path.html


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. 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 http://perldoc.perl.org/File/Basename.html


Related essays:

Python Doc Problem Example: os.path.split()split a line by regex

Page created: 2005-09.
© 2005 by Xah Lee.
Xah Signet