英文:
How to read file names from a directory specified on stdin with Go
问题
我想阅读给定目录中的所有文件,并尝试了以下代码:
package main
import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)
func main() {
    fmt.Print("directory: ")
	inBuf := bufio.NewReader(os.Stdin)
	inDir, _ := inBuf.ReadString('\n')
	strings.Replace(inDir, "\\", "/", -1)
	files, _ := ioutil.ReadDir(inDir)
	fmt.Println(files)
}
无论是否有strings.Replace这一行,它都会始终返回"[]"。有人能指出代码的问题,或者提供更简单的方法来完成这个任务吗?谢谢!
英文:
I would like to read all files from a given directory, and I tried this:
package main
import (
    "bufio"
    "fmt"
    "io/ioutil"
    "os"
    "strings"
)
func main() {
    fmt.Print("directory: ")
    inBuf := bufio.NewReader(os.Stdin)
    inDir, _ := inBuf.ReadString('\n')
    strings.Replace(inDir, "\\", "/", -1)
    files, _ := ioutil.ReadDir(inDir)
    fmt.Println(files)
}
This will always return "[]" with or without the "strings.Replace" line. Could anyone point out the problem with the code, or provide a easier way to accomplish this task? Thanks!
答案1
得分: 2
这是翻译好的内容:
它无法工作是因为你必须去掉inDir变量中的换行符,否则它会尝试列出"dirprovided\n"的内容。类似这样的代码可能有效:
func main() {
    fmt.Print("directory: ")
    inBuf := bufio.NewReader(os.Stdin)
    inDir, _ := inBuf.ReadString('\n')
    inDir = strings.Replace(inDir, "\n", "", -1)
    files, _ := ioutil.ReadDir(inDir)
    fmt.Println(files)
}
编辑:还有如上所述,打印错误而不是丢弃错误会有所帮助。这就是我解决这个问题所做的一切。
英文:
It isn't working because you must strip the \n from the inDir variable, otherwise it tries to list the contents of "dirprovided\n". Something like this might work:
func main() {
    fmt.Print("directory: ")
    inBuf := bufio.NewReader(os.Stdin)
    inDir, _ := inBuf.ReadString('\n')
    inDir = strings.Replace(inDir, "\n", "", -1)
    files, _ := ioutil.ReadDir(inDir)
    fmt.Println(files)
}
Edit: also as mentioned above, printing the errors instead of dropping will help. That's all I did to figure this one out.
答案2
得分: 0
建议的步骤:
- 使用 bufio 库从用户那里请求目录
 - 使用 os 库根据常见的路径缩写确定绝对路径
 - 使用 ioutil 库读取目录内容
 - 打印绝对路径;遍历目录内容并打印每个项目的名称
 
package main
import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)
func main() {
	// 步骤一
	scanner := bufio.NewScanner(os.Stdin)
	fmt.Print("选择一个目录:")
	scanner.Scan()
	dir := scanner.Text()
	// 步骤二
	if dir[:1] == "~" {
		dir = os.Getenv("HOME") + dir[1:]
	} else if dir[:1] != "/" {
		pwd, _ := os.Getwd()
		dir = pwd + "/" + dir
	}
	// 步骤三
	files, _ := ioutil.ReadDir(dir)
	// 步骤四
	fmt.Println("目录:", dir, "\n")
	for _, f := range files {
		fmt.Println(f.Name())
	}
}
英文:
Suggested steps:
- Request directory from user using bufio library
 - Determine absolute path based on common path abbreviations using os library
 - Read directory contents using ioutil library
 - Print absolute path; iterate through directory contents and print name of each item
 
<br>
package main
import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)
func main() {
	// STEP ONE
	scanner := bufio.NewScanner(os.Stdin)
	fmt.Print("Choose a directory: ")
	scanner.Scan()
	dir := scanner.Text()
	// STEP TWO
	if dir[:1] == "~" {
		dir = os.Getenv("HOME") + dir[1:]
	} else if dir[:1] != "/" {
		pwd, _ := os.Getwd()
		dir = pwd + "/" + dir
	}
	// STEP THREE
	files, _ := ioutil.ReadDir(dir)
	// STEP FOUR
	fmt.Println("Directory: ", dir, "\n")		
	for _, f := range files {
		fmt.Println(f.Name())
	}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论