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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

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

    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(m yPath)
    (fileBaseName, fileExtension)= os.path.splitex t(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($myPa th,
    ('\.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
    xah@xahlee.org
    ∑ http://xahlee.org/

  • Dr.Ruud

    #2
    Re: Perl-Python-a-Day: split a file full path

    Xah Lee:
    [color=blue]
    > In Perl, spliting a full path into parts is done like this:[/color]

    And then follows Perl-code that only works with an optional .html
    "extension" ,
    which is similar to the code in the File::Basename description.



    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.4 5-beta';


    and this:

    $myPath = 'x:.\example\ba sename.ext';


    (some platforms have a wd per device)


    --
    Affijn, Ruud

    "Gewoon is een tijger."

    Comment

    • matteo d'addio 81

      #3
      Re: Perl-Python-a-Day: split a file full path

      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):


      matteo

      Comment

      • Xah Lee

        #4
        Re: Perl-Python-a-Day: split a file full path

        Xah Lee wrote:[color=blue][color=green]
        > > In Perl, spliting a full path into parts is done like this:[/color][/color]

        Dr.Ruud wrote:[color=blue]
        > And then follows Perl-code that only works with an optional .html
        > "extension" ,[/color]

        Thanks for the note. I've corrected it here:


        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
        xah@xahlee.org
        ∑ http://xahlee.org/

        Comment

        Working...