how to ingrate my code to read txt file in dirctory(folder)and subdirectory? PLZ help

Reply

Join Date: Mar 2008
Posts: 16
Reputation: alivip is an unknown quantity at this point 
Solved Threads: 0
alivip alivip is offline Offline
Newbie Poster

how to ingrate my code to read txt file in dirctory(folder)and subdirectory? PLZ help

 
0
  #1
Mar 18th, 2008
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

  1. # count words in a text and show the first ten items
  2. # by decreasing frequency
  3.  
  4. # sample text for testing
  5.  
  6. import sys
  7. import string
  8. import re
  9. file = open ("arb.txt", "r")
  10. text = file.read ( )
  11. file.close ( )
  12.  
  13. word_freq = {}
  14.  
  15. word_list = text.split()
  16.  
  17. for word in word_list:
  18. # word all lower case
  19. word = word.lower()
  20. # strip any trailing period or comma
  21. word = word.rstrip('.,/"-_;\[]()')
  22. # build the dictionary
  23. count = word_freq.get(word, 0)
  24. word_freq[word] = count + 1
  25.  
  26. # create a list of (freq, word) tuples
  27. freq_list = [(freq, word) for word, freq in word_freq.items()]
  28.  
  29. # sort the list by the first element in each tuple (default)
  30. freq_list.sort(reverse=True)
  31.  
  32. for n, tup in enumerate(freq_list):
  33. # print the first ten items
  34. if n < 10:
  35. freq, word = tup
  36. print freq, word
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: how to ingrate my code to read txt file in dirctory(folder)and subdirectory? PLZ help

 
0
  #2
Mar 18th, 2008
You could check out the os and os.path modules.

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC