-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_count_2.go
61 lines (52 loc) · 1.37 KB
/
word_count_2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
// Prompt the user to enter the directory path
fmt.Print("Enter the directory path: ")
var dirPath string
fmt.Scanln(&dirPath)
// Read the directory
files, err := ioutil.ReadDir(dirPath)
if err != nil {
fmt.Println("Error:", err)
return
}
// Iterate over files in the directory
for _, fileInfo := range files {
// Check if the file is a regular file and has a .txt extension
if fileInfo.Mode().IsRegular() && strings.HasSuffix(fileInfo.Name(), ".txt") {
// Open the file
filePath := dirPath + "/" + fileInfo.Name()
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error:", err)
continue
}
defer file.Close()
// Create a scanner to read from the file
scanner := bufio.NewScanner(file)
// Initialize word count variable
wordCount := 0
// Scan through the file line by line
for scanner.Scan() {
// Split each line into words
words := strings.Fields(scanner.Text())
// Increment the word count by the number of words in the line
wordCount += len(words)
}
// Check for any errors encountered during scanning
if err := scanner.Err(); err != nil {
fmt.Println("Error:", err)
continue
}
// Print the word count for the current file
fmt.Printf("Word count for %s: %d\n", fileInfo.Name(), wordCount)
}
}
}