当前位置:网站首页>2021-05-02: given the path of a file directory, write a function
2021-05-02: given the path of a file directory, write a function
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-05-02: Given the path of a file directory , Write a function to count the number of all files in this directory and return . Hidden files count , But folders don't count .
Fuda answer 2021-05-02:
1. use filepath.Walk Method .
2. Traverse with breadth first +ioutil.
The code to use golang To write . The code is as follows :
package main
import (
"container/list"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func main() {
ret := getFileNumber1("D:\\mysetup\\gopath\\src\\sf\\newclass")
fmt.Println("1. use filepath.Walk Method :", ret)
ret = getFileNumber2("D:\\mysetup\\gopath\\src\\sf\\newclass")
fmt.Println("2. Traverse with breadth first +ioutil:", ret)
}
func getFileNumber1(folderPath string) int {
folderPath = toLinux(folderPath)
info, err := os.Lstat(folderPath)
// Not a file , It's not a folder either
if err != nil {
return 0
}
// If it's a file
if !info.IsDir() {
return 1
}
// If it's a folder
ans := 0
filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
ans++
return nil
})
// Return results
return ans
}
func getFileNumber2(folderPath string) int {
folderPath = toLinux(folderPath)
info, err := os.Lstat(folderPath)
// Not a file , It's not a folder either
if err != nil {
return 0
}
// If it's a file
if !info.IsDir() {
return 1
}
// Add folder to queue
ans := 0
queue := list.New()
queue.PushBack(folderPath)
for queue.Len() > 0 {
files, _ := ioutil.ReadDir(queue.Front().Value.(string))
for _, file := range files {
if file.IsDir() {
queue.PushBack(filepath.Join(folderPath, file.Name()))
} else {
ans++
}
}
queue.Remove(queue.Front())
}
// Return results
return ans
}
func toLinux(basePath string) string {
return strings.ReplaceAll(basePath, "\\", "/")
}The results are as follows :
***
边栏推荐
- Istio FAQ: region awareness does not take effect
- My network relationship with "apifox"
- 中国产品经理的没落:从怀恋乔布斯开始谈起
- 日志记录真没你想的那么简单
- How to obtain ECS metadata
- QTreeWidget作为单例模式以dll返回的两个问题
- MongoDB入门实战教程:学习总结目录
- Why is it easy for enterprises to fail in implementing WMS warehouse management system
- Here comes Wi Fi 7. How strong is it?
- MySQL development specification
猜你喜欢

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

存在安全隐患 部分冒险家混动版将召回
![[interview high frequency questions] sequential DP questions with difficulty of 3/5 and direct construction](/img/32/720ffa63a90cd5d37460face3fde38.png)
[interview high frequency questions] sequential DP questions with difficulty of 3/5 and direct construction

The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television

几种常见的DoS攻击

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

Still worried about missing measurements? Let's use Jacobo to calculate the code coverage

I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15

Linux record -4.22 MySQL 5.37 installation (supplementary)

构建Go命令行程序工具链
随机推荐
How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
Step by step import RHEL image to Tencent cloud
Istio practical skill: hide the automatically added server header
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
Database tools in intelij can connect but cannot display schema, tables
Wi-Fi 7 来啦,它到底有多强?
Industry cases of successful digital transformation
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
Nifi from introduction to practice (nanny level tutorial) - environment
Two problems of qtreewidget returning as DLL in singleton mode
Remain true to our original aspiration
PHP application container deployment practice
MySQL binlog
[tke] multiple ingress controllers are used in the cluster
Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
Rush for IPO, Hello, I'm in a hurry
Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现
Istio practical tips: Customize Max_ body_ size