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

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.