Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove path from filename

Tags:

go

I have trivial question.

I have string which contains a filename and it's path. How can i remove whole path? I have tried those:

line = "/some/path/to/remove/file.name" line := strings.LastIndex(line, "/") fmt.Println(line) 

It prints some strange number:

38 

I need it without last slash

Thanks a lot

like image 800
Polinux Avatar asked Dec 30 '15 10:12

Polinux


People also ask

How do I separate the filename and path in Python?

split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.

What is a filename path?

The set of names required to specify a particular file in a hierarchy of directories is called the path to the file, which you specify as a path name.


1 Answers

The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:

path := "/some/path/to/remove/file.name" file := filepath.Base(path) fmt.Println(file) 

Playground: http://play.golang.org/p/DzlCV-HC-r.

like image 174
Ainar-G Avatar answered Sep 30 '22 07:09

Ainar-G