how to ingrate my code to read text in in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name Camry and file name corolla, file name Honda contain folder accord and BMW contain file name X5

Is there way to enter name of parent folder(cars) and search in all sub folder(Toyota,Honda and BMW) and files ?

please help ASAP

code is find most frequent word in one text file and print them in decrease order
and I wont it to find most frequant word in all text files (together) under specific folder

# count words in a text and show the first ten items
# by decreasing frequency
 
# sample text for testing

import sys
import string
import re
file = open ("arb.txt", "r")
text = file.read ( )
file.close ( )
 
word_freq = {}
 
word_list = text.split()
 
for word in word_list:
    # word all lower case
    word = word.lower()
    # strip any trailing period or comma
    word = word.rstrip('.,/"-_;\[]()')
    # build the dictionary
    count = word_freq.get(word, 0)
    word_freq[word] = count + 1
 
# create a list of (freq, word) tuples
freq_list = [(freq, word) for word, freq in word_freq.items()]
 
# sort the list by the first element in each tuple (default)
freq_list.sort(reverse=True)
 
for n, tup in enumerate(freq_list):
    # print the first ten items
    if n < 10:
        freq, word = tup
        print freq, word

Dani AI

Generated

A compact, practical approach for ’s goal (enter a parent folder and count words across every text file in that folder tree) is: recurse the tree, open each text file safely, extract normalized tokens with a regex, and aggregate counts into a single Counter. ’s pointer to filesystem helpers is the right direction; the snippet below uses the modern pathlib API and collections.Counter so you get a readable, robust solution that scales better than reading one hard-coded file.

from pathlib import Path
import re
from collections import Counter

def count_words_in_tree(root_folder):
    root = Path(root_folder)
    counts = Counter()
    for p in root.rglob('*.txt'):
        try:
            with p.open(encoding='utf-8', errors='ignore') as fh:
                for line in fh:
                    tokens = re.findall(r"\b\w+\b", line.lower())
                    counts.update(tokens)
        except OSError:
            continue
    return counts

if __name__ == "__main__":
    c = count_words_in_tree("cars")
    for word, freq in c.most_common(10):
        print(freq, word)

Notes and troubleshooting:

  • Read files line-by-line to avoid high memory use on large files.
  • Use encoding and errors='ignore' (or try multiple encodings) when files have mixed encodings.
  • Adjust the regex to handle apostrophes, hyphens or non-ASCII words if needed.
  • To skip non-text files, filter by .suffix or try opening and skip on failure.
  • For stop‑words, stemming, or more advanced tokenizing, consider NLTK or spaCy.

Relevant docs: pathlib.Path.rglob, collections.Counter, re.findall.

You could check out the os and os.path modules.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.