By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to read from parent directory(folder) files AND how to read non enlish NEED HELP

P: 17
Is python provide search 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 corola, 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 ?

how can I intgreat cod to be user interface (buttun ,text box etc) and let user to enter find most X (10,20,30 etc) frequency word
and how to let user to writ parent file name to search in its container (files and folders)



Expand|Select|Wrap|Line Numbers
  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.     v1.set(text1.get(1.0, tk.END))
  10.     text1.delete(1.0, tk.END)
  11.     file = open ("arb.txt", "r")
  12.     text = file.read ( )
  13.     file.close ( )
  14.  
  15.     word_freq = {}
  16.  
  17.     word_list = text.split()
  18.  
  19.     for word in word_list:
  20.         # word all lower case
  21.         word = word.lower()
  22.         # strip any trailing period or comma
  23.         word = word.rstrip('.,/"-_;\[]()')
  24.         # build the dictionary
  25.         count = word_freq.get(word, 0)
  26.         word_freq[word] = count + 1
  27.  
  28.     # create a list of (freq, word) tuples
  29.     freq_list = [(freq, word) for word, freq in word_freq.items()]
  30.  
  31.     # sort the list by the first element in each tuple (default)
  32.     freq_list.sort(reverse=True)
  33.  
  34.     for n, tup in enumerate(freq_list):
  35.         # print the first ten items
  36.         if n < 10:
  37.             text1.insert(tk.INSERT, freq)
  38.             text1.insert(tk.INSERT, word)
  39.             text1.insert(tk.INSERT, "\n")
  40.             freq, word = tup
  41.             print freq, word
  42. root = tk.Tk(className = " most_frequant_word")
  43.  
  44.  
  45. # text entry field, width=width chars, height=lines text
  46.  
  47.  
  48. text1 = tk.Text(root, width=50, height=20, bg='green')
  49. text1.pack()
  50. # function listed in command will be executed on button click
  51. button1 = tk.Button(root, text='result', command=most_frequant_word)
  52. button1.pack(pady=5)
  53.  
  54. # define a variable to hold the label text
  55. v1 = tk.StringVar()
  56. # label text will always be the textvariable's value
  57. # width/height in char size
  58. label1 = tk.Label(root, textvariable=v1, width=50, height=20)
  59. label1.pack(pady=5)
  60.  
  61. # start cursor in text1.
  62. text1.focus()
  63. root.mainloop()  
but unfortunately when I wont to search in (not English CHAR) file it will not read it probably it print text like
3
28
11
11
10
10
7
6
5
5



SAMPLE FILE IN ATTACH

I use

Expand|Select|Wrap|Line Numbers
  1.             text1.insert(tk.INSERT, freq)
  2.             text1.insert(tk.INSERT, word)
  3.             text1.insert(tk.INSERT, "\n")  
  4.  
to inset to the text
pleas I need your help for this and previous one
Attached Files
File Type: txt arb.txt (7.7 KB, 356 views)
Mar 17 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.