从文件名中删除路径

huangapple go评论87阅读模式
英文:

Remove path from filename

问题

我有一个简单的问题。

我有一个包含文件名和路径的字符串。如何删除整个路径?我尝试了以下方法:

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

它打印出一个奇怪的数字:

38

我需要它没有最后的斜杠。

非常感谢。

英文:

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

答案1

得分: 132

数字是字符串中最后一个斜杠的索引。如果你想获取文件的基本名称,请使用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

英文:

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.

答案2

得分: 26

你可以在playground中尝试一下!

dir, file := filepath.Split("/some/path/to/remove/file.name")
fmt.Println("Dir:", dir)   //Dir: /some/path/to/remove/
fmt.Println("File:", file) //File: file.name
英文:

<b>You can try it in the playground!</b><br/>

dir, file := filepath.Split(&quot;/some/path/to/remove/file.name&quot;)
fmt.Println(&quot;Dir:&quot;, dir)   //Dir: /some/path/to/remove/
fmt.Println(&quot;File:&quot;, file) //File: file.name

答案3

得分: 9

另一个选项:

package main
import "path"

func main() {
   line := "/some/path/to/remove/file.name"
   line = path.Base(line)
   println(line == "file.name")
}

https://golang.org/pkg/path#Base

英文:

Another option:

package main
import &quot;path&quot;

func main() {
   line := &quot;/some/path/to/remove/file.name&quot;
   line = path.Base(line)
   println(line == &quot;file.name&quot;)
}

https://golang.org/pkg/path#Base

答案4

得分: 2

如果你想要去除文件名的基本路径,你可以使用Dir函数,它在这里有详细的文档说明:https://golang.org/pkg/path/filepath/#Dir

引用他们文档的一部分:

Dir函数返回路径除去最后一个元素的部分,通常是路径的目录。在去除最后一个元素后,Dir函数会对路径进行清理,并移除尾部的斜杠。

此外,在他们的文档中,运行以下代码:

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println("On Unix:")
	fmt.Println(filepath.Dir("/foo/bar/baz.js"))
	fmt.Println(filepath.Dir("/foo/bar/baz"))
	fmt.Println(filepath.Dir("/foo/bar/baz/"))
	fmt.Println(filepath.Dir("/dirty//path///"))
	fmt.Println(filepath.Dir("dev.txt"))
	fmt.Println(filepath.Dir("../todo.txt"))
	fmt.Println(filepath.Dir(".."))
	fmt.Println(filepath.Dir("."))
	fmt.Println(filepath.Dir("/"))
	fmt.Println(filepath.Dir(""))
}

将会得到以下输出:

On Unix:
/foo/bar
/foo/bar
/foo/bar/baz
/dirty/path
.
..
.
.
/
.

你可以在这里自己尝试一下:https://play.golang.org/p/huk3EmORFw5

如果你想要获取不包含基本路径的文件名,@Ainar-G已经给出了足够的答案。

英文:

If you want the base path without the fileName, you can use Dir, which is documented here: https://golang.org/pkg/path/filepath/#Dir

Quoting part of their documentation:
> Dir returns all but the last element of path, typically the path's directory. After dropping the final element, Dir calls Clean on the path and trailing slashes are removed.

Also from their documentation, running this code:

package main

import (
	&quot;fmt&quot;
	&quot;path/filepath&quot;
)

func main() {
	fmt.Println(&quot;On Unix:&quot;)
	fmt.Println(filepath.Dir(&quot;/foo/bar/baz.js&quot;))
	fmt.Println(filepath.Dir(&quot;/foo/bar/baz&quot;))
	fmt.Println(filepath.Dir(&quot;/foo/bar/baz/&quot;))
	fmt.Println(filepath.Dir(&quot;/dirty//path///&quot;))
	fmt.Println(filepath.Dir(&quot;dev.txt&quot;))
	fmt.Println(filepath.Dir(&quot;../todo.txt&quot;))
	fmt.Println(filepath.Dir(&quot;..&quot;))
	fmt.Println(filepath.Dir(&quot;.&quot;))
	fmt.Println(filepath.Dir(&quot;/&quot;))
	fmt.Println(filepath.Dir(&quot;&quot;))

}

will give you this output:

On Unix:

 /foo/bar
 /foo/bar
 /foo/bar/baz
 /dirty/path
 .
 ..
 .
 .
 /
 .

Try it yourself here:

https://play.golang.org/p/huk3EmORFw5

If you instead want to get the fileName without the base path, @Ainar-G has sufficiently answered that.

huangapple
  • 本文由 发表于 2015年12月30日 18:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/34527672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定