I integrat program to be GUI using Tkinter I try browser direction
as you can see - # a look at the Tkinter Text widget
-
-
# use ctrl+c to copy, ctrl+x to cut selected text,
-
-
# ctrl+v to paste, and ctrl+/ to select all
-
# count words in a text and show the first ten items
-
# by decreasing frequency
-
-
import Tkinter as tk
-
import os, glob
-
import sys
-
import string
-
import re
-
import tkFileDialog
-
def most_frequant_word():
-
a= tkFileDialog.askdirectory()
-
browser= os.listdir(a)
-
-
-
for root, dirs, files in os.walk(browser):
-
print 'Looking into %s' % root.split('\\')[-1]
-
print 'Found %d dirs and %d files' % (len(dirs), len(files))
-
-
for idx, file in enumerate(files):
-
ff = open (os.path.join(root, file), "r")
-
text = ff.read ( )
-
ff.close ( )
-
-
word_list = text.strip().split()
-
-
for word in word_list:
-
word = word.lower().rstrip('.,/"-_;\\[]()')
-
-
if word.isalpha():
-
# 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:
-
print "%s times: %s" % tup
-
text1.insert(tk.INSERT, freq)
-
text1.insert(tk.INSERT, word)
-
text1.insert(tk.INSERT, "\n")
-
-
raw_input('\nHit enter to exit')
-
-
root = tk.Tk(className = " most_frequant_word")
-
# text entry field, width=width chars, height=lines text
-
v1 = tk.StringVar()
-
text1 = tk.Text(root, width=50, height=20, bg='green')
-
text1.pack()
-
# function listed in command will be executed on button click
-
button1 = tk.Button(root, text='result', command=most_frequant_word)
-
button1.pack(pady=5)
-
text1.focus()
-
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
1 2386
I fix the error now
but
it will not insert to the textbox
it just print then hanging - # a look at the Tkinter Text widget
-
-
# use ctrl+c to copy, ctrl+x to cut selected text,
-
-
# ctrl+v to paste, and ctrl+/ to select all
-
# count words in a text and show the first ten items
-
# by decreasing frequency
-
-
import Tkinter as tk
-
import os, glob
-
import sys
-
import string
-
import re
-
import tkFileDialog
-
def most_frequant_word():
-
browser= tkFileDialog.askdirectory()
-
#browser= os.listdir(a)
-
-
-
for root, dirs, files in os.walk(browser):
-
print 'Looking into %s' % root.split('\\')[-1]
-
print 'Found %d dirs and %d files' % (len(dirs), len(files))
-
#text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
-
#text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
-
for idx, file in enumerate(files):
-
print 'File #%d: %s' % (idx + 1, file)
-
#text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
-
ff = open (os.path.join(root, file), "r")
-
text = ff.read ( )
-
ff.close ( )
-
word_freq = {}
-
-
word_list = text.strip().split()
-
-
for word in word_list:
-
word = word.lower().rstrip('.,/"-_;\\[]()')
-
-
if word.isalpha():
-
# 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 < 50:
-
print "%s times: %s" % tup
-
text1.insert(tk.INSERT, freq)
-
text1.insert(tk.INSERT, word)
-
text1.insert(tk.INSERT, "\n")
-
-
raw_input('\nHit enter to exit')
-
-
root = tk.Tk(className = " most_frequant_word")
-
# text entry field, width=width chars, height=lines text
-
v1 = tk.StringVar()
-
text1 = tk.Text(root, width=50, height=20, bg='green')
-
text1.pack()
-
# function listed in command will be executed on button click
-
button1 = tk.Button(root, text='Brows', command=most_frequant_word)
-
button1.pack(pady=5)
-
text1.focus()
-
root.mainloop()
code try to insert textbox - print "%s times: %s" % tup
-
text1.insert(tk.INSERT, freq)
-
text1.insert(tk.INSERT, word)
-
text1.insert(tk.INSERT, "\n")
when I wont to insert fil name and directory to the textbox it will hang also
code is comment - print 'Looking into %s' % root.split('\\')[-1]
-
print 'Found %d dirs and %d files' % (len(dirs), len(files))
-
#text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
-
#text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
-
for idx, file in enumerate(files):
-
print 'File #%d: %s' % (idx + 1, file)
-
#text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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'....
|
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...
|
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 =...
|
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....
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
| |