Search the web
Sign In
New User? Sign Up
perl-python
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
split a file fullpath into parts   Message List  
Reply | Forward Message #116 of 127 |
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/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.

For the official doc, type in command line: “perldoc File::Path”.
----------
this post is archived at:
http://xahlee.org/perl-python/split_fullpath.html





Mon Oct 17, 2005 12:32 am

p0lyglut
Online Now Online Now
Send Email Send Email

Forward
Message #116 of 127 |
Expand Messages Author Sort by Date

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...
xah lee
p0lyglut
Online Now Send Email
Oct 17, 2005
12:44 am
Advanced

Copyright 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help