我正在編寫一個例程來遍歷目錄樹并為我找到的每個文件創建一個數字簽名(salted-hash)。在測試它時,我得到了這種奇怪的行為 - 如果我給程序一個目錄“上方”的根路徑,程序可以遍歷樹并打印出文件名,但是如果我嘗試打開文件以讀取它的字節,我在例程找到的文件上獲取錯誤消息“沒有這樣的文件或目錄” - 不確定這里給出了什么。Walk() 例程如何“看到”文件,但 ioutil.ReadFile() 無法找到它?示例代碼:// start with path higher up the tree, say $HOMEfunc doHashWalk(dirPath string) { err := filepath.Walk(dirPath, walkFn) // Check err here}func walkFn(path string, fi os.FileInfo, err error) (e error) { if !fi.IsDir() { // if the first character is a ".", then skip it as it's a hidden file if strings.HasPrefix(fi.Name(), ".") { return nil } // read in the file bytes -> get the absolute path fullPath, err := filepath.Abs(fi.Name()) if err != nil { log.Printf("Abs Err: %s", err) } // THIS ALWAYS FAILED WITH ERROR bytes, err := ioutil.ReadFile(fullPath) // <-- (fi.Name() also doesn't work) if err != nil { log.Printf("Err: %s, Bytes: %d", err, len(bytes)) } // create the salted hash ... } return nil}
- 2 回答
- 0 關注
- 332 瀏覽
添加回答
舉報
0/150
提交
取消