parse file name's basename

7,191 views
Skip to first unread message

dlin

unread,
Apr 18, 2012, 6:08:43 AM4/18/12
to golan...@googlegroups.com
for file name as a/b.c,  I want to extract b.

Here is my stupid code, is there any better method?

import "path"

inFName := "a/b.c"
fName := path.Base(inFName)
extName := path.Ext(inFName)
bName := fName[:len(fName)-len(extName)]

//bName will be "b"


Robert Bloomquist

unread,
Apr 18, 2012, 7:19:43 AM4/18/12
to golan...@googlegroups.com
Perhaps something like this?

import (
    "path"
    "strings"
)

str := "your/path.here"
i, j := strings.LastIndex(str, "/"), strings.LastIndex(str, path.Ext(str))
name := str[i:j]

Robert

LRN

unread,
Apr 18, 2012, 8:59:50 AM4/18/12
to golan...@googlegroups.com


On Wednesday, April 18, 2012 3:19:43 PM UTC+4, Robert Bloomquist wrote:
On Wednesday, April 18, 2012 6:08:43 AM UTC-4, dlin wrote:
for file name as a/b.c,  I want to extract b.

Here is my stupid code, is there any better method?

import "path"

inFName := "a/b.c"
fName := path.Base(inFName)
extName := path.Ext(inFName)
bName := fName[:len(fName)-len(extName)]

//bName will be "b"


Perhaps something like this?

import (
    "path"
    "strings"
)

str := "your/path.here"
i, j := strings.LastIndex(str, "/"), strings.LastIndex(str, path.Ext(str))
name := str[i:j]

You forgot that on some rare and obscure operating systems that are not really used by anyone these days, not even on desktops, path separator could be "\\" instead of "/".
The use of
path.Base() abstracts that detail.

 

Kyle Lemons

unread,
Apr 18, 2012, 1:45:03 PM4/18/12
to dlin, golan...@googlegroups.com
Looks reasonable to me.  Put it in a function if you need to call it a lot.

dlin

unread,
Apr 19, 2012, 10:34:25 PM4/19/12
to golan...@googlegroups.com, dlin
Well, I just wonder, if Go provide a function like python:


os.path.splitext(path)

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns('.cshrc', '').


On Thursday, April 19, 2012 1:45:03 AM UTC+8, Kyle Lemons wrote:
Looks reasonable to me.  Put it in a function if you need to call it a lot.

Kyle Lemons

unread,
Apr 20, 2012, 12:15:27 PM4/20/12
to dlin, golan...@googlegroups.com
On Thu, Apr 19, 2012 at 7:34 PM, dlin <dli...@gmail.com> wrote:
Well, I just wonder, if Go provide a function like python:
Nope.

Ugorji Nwoke

unread,
Apr 20, 2012, 12:27:46 PM4/20/12
to golan...@googlegroups.com, dlin
BTW, I think you want to use path/filepath, not path i.e.

import "path/filepath"

inFName := "a/b.c"
fName := filepath.Base(inFName)
extName := filepath.Ext(inFName)
bName := fName[:len(fName)-len(extName)]

The main difference between path and filepath, is that path uses "/" as separator, but filepath is more OS-specific, so will use the OS-specific separators, etc (e.g. / for files, : for paths in POSIX, and \ for files, ; for paths in windows, etc).
Reply all
Reply to author
Forward
0 new messages