内容概述

package main import ( "encoding/csv" "fmt" "os" "path/filepath" "container/list" ) var outputFileName string = "filesName.csv" func CheckErr(err error) { if nil != err { panic(err) } } func GetFull

语音解读

图文解释

package main

import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"container/list"
)
var outputFileName string = "filesName.csv"

func CheckErr(err error) {
if nil != err {
panic(err)
}
}

func GetFullPath(path string) string {
absolutePath,_ := filepath.Abs(path)
return absolutePath
}

func PrintFilesName(path string) {
fullPath := GetFullPath(path)

listStr := list.New()

filepath.Walk(fullPath,func(path string,fi os.FileInfo,err error) error {
if nil == fi {
return err
}
if fi.IsDir() {
return nil
}

name := fi.Name()
if outputFileName != name{
listStr.PushBack(name)
}

return nil
})

OutputFilesName(listStr)
}

func ConvertToSlice(listStr *list.List)[]string{
sli := []string{}
for el:= listStr.Front(); nil != el; el= el.Next(){
sli = append(sli,el.Value.(string))
}

return sli
}

func OutputFilesName(listStr *list.List) {
files := ConvertToSlice(listStr)
//sort.StringSlice(files).sort()// sort  

f,err := os.Create(outputFileName)
//f,err := os.OpenFile(outputFileName,os.O_APPEND | os.O_CREATE,os.ModeAppend)
CheckErr(err)
defer f.Close()

f.WriteString("\xEF\xBB\xBF")
writer := csv.NewWriter(f)

length := len(files)
for i:= 0; i < length; i++{
writer.Write([]string{files[i]})
}

writer.Flush()
}

func main() {
var path string
if len(os.Args) > 1 {
path = os.Args[1]
} else {
path,_ = os.Getwd()
}
PrintFilesName(path)

fmt.Println("done!")
}

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给好友。