How do you get full paths of all files in a directory in Go?

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

How do you get full paths of all files in a directory in Go?

问题

我正在使用Go语言遍历目录中的所有文件。以下是我的代码:

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	printFiles(".")
}

func printFiles(dir string) {
	fileInfos, err := ioutil.ReadDir(dir)
	if err != nil {
		fmt.Println("访问目录时出错:", err)
	}

	for _, file := range fileInfos {
		fmt.Printf("%T: %+v\n", file, file)
	}
}

当我运行这段代码时,输出如下:

*os.fileStat: &{name:main.go sys:{FileAttributes:32 CreationTime:{LowDateTime:2993982878 HighDateTime:30613689} LastAccessTime:{LowDateTime:2993982878 HighDateTime:30613689} LastWriteTime:{LowDateTime:4004986069 HighDateTime:30613714} FileSizeHigh:0 FileSizeLow:320} pipe:false Mutex:{state:0 sema:0} path:C:\Users\Prakhar.Mishra\go\src\mistdatafilter\main.go vol:0 idxhi:0 idxlo:0}

我可以看到一个名为path的属性,但我无法访问它(可能是因为它的首字母是小写?)。请问有人可以告诉我如何获取给定文件夹中所有文件的完整路径吗?

英文:

I am using Go to iterate over all the files in a directory. This is how I am doing it:

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	printFiles(".")
}

func printFiles(dir string) {
	fileInfos, err := ioutil.ReadDir(dir)
	if err != nil {
		fmt.Println("Error in accessing directory:", err)
	}

	for _, file := range fileInfos {
		fmt.Printf("%T: %+v\n", file, file)
	}
}

When I run this code, this is the output I get:

*os.fileStat: &{name:main.go sys:{FileAttributes:32 CreationTime:{LowDateTime:2993982878 HighDateTime:30613689} LastAccessTime:{LowDateTime:2993982878 HighDateTime:30613689} LastWriteTime:{LowDateTime:4004986069 HighDateTime:30613714} FileSizeHigh:0 FileSizeLow:320} pipe:false Mutex:{state:0 sema:0} path:C:\Users\Prakhar.Mishra\go\src\mistdatafilter\main.go vol:0 idxhi:0 idxlo:0}

I can see a property named path, but I can't access it (due to small case initial, I suppose?). Can anyone please tell me how to get full file path of all the files in a given folder?

答案1

得分: 17

如果你想看到完整的路径,你应该从完整路径开始。.是一个相对路径。

你可以使用os.Getwd来获取工作路径:

path, err := os.Getwd()
// 处理错误
printFiles(path)

剩下的就是将文件名附加到目录路径上。你应该使用path/filepath包来实现:

for _, file := range fileInfos {
    fmt.Println(filepath.Join(path, file.Name()))
}
英文:

If you want to see a full path, you should start with a full path. . is a relative path.

You can get the working path with os.Getwd

path, err := os.Getwd()
// handle err
printFiles(path)

The rest is simply appending the file name to the directory path. You should use the path/filepath package for that:

for _, file := range fileInfos {
    fmt.Println(filepath.Join(path, file.Name())
}

答案2

得分: 3

你也可以使用标准库中的filepath.Abs方法。

import (
    "fmt"
    "os"
    "path/filepath"
)

files, _ = os.ReadDir(dir)
path, _ := filepath.Abs(dir)
for _, file := range files {
    fmt.Println(filepath.Join(path, file.Name())
}
英文:

You can also use the filepath.Abs method from the standard library

import (
    "fmt"
    "os"
    "path/filepath"
)

files, _ = os.ReadDir(dir)
path, _ := filepath.Abs(dir)
for _, file := range files {
    fmt.Println(filepath.Join(path, file.Name())
}

答案3

得分: 1

你已经有了目录路径,只需将它们附加到文件名上,类似于:

files, err := ioutil.ReadDir(dirPath)
check(err)

for _, file := range files {
    fmt.Println(dirPath + file.Name())
}
英文:

You already have the directory path simply append them to the file name something like:

files, err := ioutil.ReadDir(dirPath)
check(err)

for _, file := range files {
    fmt.Println(dirPath + file.Name())
}

huangapple
  • 本文由 发表于 2017年8月29日 22:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/45941821.html
匿名

发表评论

匿名网友

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

确定