472,985 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

try to browsing directory using TKinter but give error please help

17
I integrat program to be GUI using Tkinter I try browser direction
as you can see


Expand|Select|Wrap|Line Numbers
  1. # a look at the Tkinter Text widget
  2.  
  3. # use ctrl+c to copy, ctrl+x to cut selected text,
  4.  
  5. # ctrl+v to paste, and ctrl+/ to select all
  6.   # count words in a text and show the first ten items
  7.  # by decreasing frequency
  8.  
  9. import Tkinter as tk
  10. import os, glob
  11. import sys
  12. import string
  13. import re
  14. import tkFileDialog      
  15. def most_frequant_word():    
  16.  a= tkFileDialog.askdirectory()
  17.  browser= os.listdir(a)
  18.  
  19.  
  20.  for root, dirs, files in os.walk(browser):
  21.     print 'Looking into %s' % root.split('\\')[-1]
  22.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  23.  
  24.     for idx, file in enumerate(files):
  25.      ff = open (os.path.join(root, file), "r")
  26.      text = ff.read ( )
  27.      ff.close ( )
  28.  
  29.      word_list = text.strip().split()
  30.  
  31.      for word in word_list:
  32.       word = word.lower().rstrip('.,/"-_;\\[]()')
  33.  
  34.       if word.isalpha():
  35.                 # build the dictionary
  36.        count = word_freq.get(word, 0)
  37.        word_freq[word] = count + 1
  38.  
  39.        # create a list of (freq, word) tuples
  40.        freq_list = [(freq, word) for word, freq in word_freq.items()]
  41.  
  42.        # sort the list by the first element in each tuple (default)
  43.        freq_list.sort(reverse=True)
  44.  
  45.      for n, tup in enumerate(freq_list):
  46.     # print the first ten items
  47.       if n < 10:
  48.         print "%s times: %s" % tup
  49.         text1.insert(tk.INSERT, freq)
  50.         text1.insert(tk.INSERT, word)
  51.         text1.insert(tk.INSERT, "\n")
  52.  
  53.  raw_input('\nHit enter to exit')
  54.  
  55. root = tk.Tk(className = " most_frequant_word")
  56. # text entry field, width=width chars, height=lines text
  57. v1 = tk.StringVar()
  58. text1 = tk.Text(root, width=50, height=20, bg='green')
  59. text1.pack()
  60. # function listed in command will be executed on button click
  61. button1 = tk.Button(root, text='result', command=most_frequant_word)
  62. button1.pack(pady=5)
  63. text1.focus()
  64. root.mainloop()
which find most 10 words frequancy by search all files in specfic directory

but give me this error

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "C:\Documents and Settings\Administrator\Desktop\ICS482\hw3\programA li.py", line 21, in most_frequant_word
for root, dirs, files in os.walk(browser):
File "C:\Python25\lib\os.py", line 285, in walk
names = listdir(top)
TypeError: coercing to Unicode: need string or buffer, list found
could you please help me to solve this problem
Mar 20 '08 #1
1 2386
alivip
17
I fix the error now
but
it will not insert to the textbox
it just print then hanging

Expand|Select|Wrap|Line Numbers
  1. # a look at the Tkinter Text widget
  2.  
  3. # use ctrl+c to copy, ctrl+x to cut selected text,
  4.  
  5. # ctrl+v to paste, and ctrl+/ to select all
  6.   # count words in a text and show the first ten items
  7.  # by decreasing frequency
  8.  
  9. import Tkinter as tk
  10. import os, glob
  11. import sys
  12. import string
  13. import re
  14. import tkFileDialog      
  15. def most_frequant_word():    
  16.  browser= tkFileDialog.askdirectory()
  17.  #browser= os.listdir(a)
  18.  
  19.  
  20.  for root, dirs, files in os.walk(browser):
  21.     print 'Looking into %s' % root.split('\\')[-1]
  22.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  23.     #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
  24.     #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
  25.     for idx, file in enumerate(files):
  26.      print 'File #%d: %s' % (idx + 1, file)
  27.       #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
  28.      ff = open (os.path.join(root, file), "r")
  29.      text = ff.read ( )
  30.      ff.close ( )
  31.      word_freq = {}
  32.  
  33.      word_list = text.strip().split()
  34.  
  35.      for word in word_list:
  36.       word = word.lower().rstrip('.,/"-_;\\[]()')
  37.  
  38.       if word.isalpha():
  39.                 # build the dictionary
  40.        count = word_freq.get(word, 0)
  41.        word_freq[word] = count + 1
  42.  
  43.        # create a list of (freq, word) tuples
  44.        freq_list = [(freq, word) for word, freq in word_freq.items()]
  45.  
  46.        # sort the list by the first element in each tuple (default)
  47.        freq_list.sort(reverse=True)
  48.  
  49.      for n, tup in enumerate(freq_list):
  50.     # print the first ten items
  51.       if n < 50:
  52.         print "%s times: %s" % tup
  53.         text1.insert(tk.INSERT, freq)
  54.         text1.insert(tk.INSERT, word)
  55.         text1.insert(tk.INSERT, "\n")
  56.  
  57.  raw_input('\nHit enter to exit')
  58.  
  59. root = tk.Tk(className = " most_frequant_word")
  60. # text entry field, width=width chars, height=lines text
  61. v1 = tk.StringVar()
  62. text1 = tk.Text(root, width=50, height=20, bg='green')
  63. text1.pack()
  64. # function listed in command will be executed on button click
  65. button1 = tk.Button(root, text='Brows', command=most_frequant_word)
  66. button1.pack(pady=5)
  67. text1.focus()
  68. root.mainloop()
code try to insert textbox
Expand|Select|Wrap|Line Numbers
  1.  print "%s times: %s" % tup
  2.         text1.insert(tk.INSERT, freq)
  3.         text1.insert(tk.INSERT, word)
  4.         text1.insert(tk.INSERT, "\n")
when I wont to insert fil name and directory to the textbox it will hang also
code is comment

Expand|Select|Wrap|Line Numbers
  1. print 'Looking into %s' % root.split('\\')[-1]
  2.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  3.     #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
  4.     #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
  5.     for idx, file in enumerate(files):
  6.      print 'File #%d: %s' % (idx + 1, file)
  7.       #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
Mar 21 '08 #2

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

Similar topics

1
by: Dfenestr8 | last post by:
Earlier I posted asking for a file browsing script, for a GUI app I'd written. Basically, I was after a graphical tool with which I could search for files on my disk, and would return the path of a...
4
by: Peter G Carswell | last post by:
Good Morning. I am new to Tkinter. I have been testing the installation of Tkinter through the python web site. The first two test steps give no errors, 'import _tkinter' and 'import Tkinter'....
0
by: John Caruthers | last post by:
HELP! This problem is driving me crazy. All other web sites that are being served out through this server are working, including sites and virtual directories that are under the Default Web...
26
by: Dan Nash | last post by:
Hi guys I have a page that is *supposed* to list the directories on the server. Here's the code... folderspec = server.MapPath("./images/") set fso =...
9
by: Benny Ng | last post by:
Hi,all, How to let the sub-directory to avoid the authentication control from Root's webconfig? I heard that we can add a new web.config to the sub-directory. And then we can slove the problem....
6
by: Dreamcatcher | last post by:
Hello, I'm trying to learn some C, reading my book, Beginning Linux Programming I came across the following program. The program is supposed to walk through directory's and print all its...
1
by: WolfsonNYC | last post by:
Anyone know how to enable Directory Browsing using the Cassini web server on .Net 2.0 ? Right now it says HTTP Error 403 - Forbidden when I go to a folder on my web site. Thanks, JW
1
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call...
4
by: 13gurpreetsingh | last post by:
hi friends, my program demands that we need to list the information in all the files present in a directory ,say xxxx and the files present in all the subdirectories in this xxxx directory. please...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.