dongtan7418 2015-05-04 21:55
浏览 125
已采纳

从FileInfo打开文件

In golang, if I have an os.FileInfo, is there any way to open an *os.File from that by itself without the original path?

Let's say I had something like this:

package main

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

var files []os.FileInfo

func walker(path string, info os.FileInfo, err error) error {
    if strings.HasSuffix(info.Name(), ".txt") {
        files = append(files, info)
    }
    return nil
}

func main() {
    err := filepath.Walk("/tmp/foo", walker)
    if err != nil {
        println("Error", err)
    } else {
        for _, f := range files {
            println(f.Name())
            // This is where we'd like to open the file
        }
    }
}

Is there a way to convert FileInfo to * File? The code I'm actually working with isn't based on filepath.Walk; but I do get an []os.FileInfo slice back. I still have the root directory, and the file name, but it seems like any further sub-tree information has gone by this stage.

  • 写回答

3条回答 默认 最新

  • dpzo13732 2015-05-04 22:26
    关注

    No. The FileInfo interface simply does not expose the path and all provided methods in the os and ioutil packages accept the pathname as a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)