当前位置:网站首页>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 :
***
边栏推荐
- Two problems of qtreewidget returning as DLL in singleton mode
- Flink Kubernetes Application部署
- Junit5中的参数化测试(Parameterized Tests)指南
- 打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
- How to efficiently transfer enterprise business data?
- Efficient tools commonly used by individuals
- leetcode 139. Word break word split (medium)
- Recommend several super practical data analysis tools
- [cloud native | kubernetes chapter] Introduction to kubernetes Foundation (III)
- How to implement SQLSERVER database migration in container
猜你喜欢

60 个神级 VS Code 插件!!

MongoDB入门实战教程:学习总结目录

用 Oasis 开发一个跳一跳(一)—— 场景搭建

CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021

Why is it easy for enterprises to fail in implementing WMS warehouse management system

Several common DoS attacks

【面试高频题】难度 3/5,可直接构造的序列 DP 题

【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记

Database tools in intelij can connect but cannot display schema, tables

Nifi from introduction to practice (nanny level tutorial) - environment
随机推荐
微信公众号调试与Natapp环境搭建
Reference to junit5 test framework in gradle
Leetcode 139. Mot break word Split (medium)
Is it safe for futures companies to open accounts
Istio practical tips: Customize Max_ body_ size
2021-04-24: handwriting Code: topology sorting.
个人常用的高效工具
Detailed explanation of estab of Stata regression table output
Design of CAN bus controller based on FPGA (Part 2)
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
QTreeWidget作为单例模式以dll返回的两个问题
QoS Technology in network
VNC Viewer方式的远程连接树莓派
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
构建Go命令行程序工具链
Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
一文理解OpenStack网络
Is it safe to open an account for flush stock on mobile phone!
Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
Junit5中的参数化测试(Parameterized Tests)指南