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

Discussion in 'Perl Misc' started by Xah Lee, Oct 17, 2005.

  1. Xah Lee

    Xah Lee Guest

    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

    Schemers, a scsh version will be appreciated.

    Xah

    ∑ http://xahlee.org/
    Xah Lee, Oct 17, 2005
    #1
    1. Advertising

  2. Xah Lee

    Dr.Ruud Guest

    Xah Lee:

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


    And then follows Perl-code that only works with an optional .html
    "extension",
    which is similar to the code in the File::Basename description.
    http://www.perl.com/doc/manual/html/lib/File/Basename.html


    It is best practice to derive and store the normalized (or absolute)
    path, because relative paths can get loose so will get loose.


    Consider this:

    $myPath = './example/basename.ext';


    and this:

    $myPath = './example/filename.1.23.45-beta';


    and this:

    $myPath = 'x:.\example\basename.ext';


    (some platforms have a wd per device)


    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Oct 17, 2005
    #2
    1. Advertising

  3. Hello, I'm a cs student from Milano (Italy).
    I don't use scsh fequently but this should work:

    (open srfi-11 ;let-values
    srfi-28) ;format

    (define my-path "/Users/t/web/perl-python/I_Love_You.html")

    (let-values (((dir-name
    file-base-name
    file-extension) (parse-file-name my-path)))

    (format "~a~%" dir-name) ;/Users/t/web/perl-python/
    (format "~a~%" file-base-name) ;I_Love_You
    (format "~a~%" file-extension)) ;.html

    You can find more useful function here (scsh reference manual):
    http://www.scsh.net/docu/html/man-Z-H-6.html#node_sec_5.1

    matteo
    matteo d'addio 81, Oct 17, 2005
    #3
  4. Xah Lee

    Xah Lee Guest

    Xah Lee wrote:
    > > In Perl, spliting a full path into parts is done like this:


    Dr.Ruud wrote:
    > And then follows Perl-code that only works with an optional .html
    > "extension",


    Thanks for the note. I've corrected it here:
    http://xahlee.org/perl-python/split_fullpath.html

    namely:
    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 ('\.[^.]+$').

    Xah

    ∑ http://xahlee.org/
    Xah Lee, Oct 17, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    3
    Views:
    738
    Xah Lee
    Oct 17, 2005
  2. Andy
    Replies:
    1
    Views:
    678
  3. Replies:
    2
    Views:
    262
  4. dude
    Replies:
    5
    Views:
    1,681
    Jussi Piitulainen
    Mar 17, 2011
  5. Rob Redmon

    Convert day of year to month, day

    Rob Redmon, Apr 4, 2008, in forum: Ruby
    Replies:
    7
    Views:
    278
    Alex Wayne
    Apr 4, 2008
Loading...

Share This Page