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,H onda 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
Mar 18 '08
11 2137
thank you very much M.r jlm699 it is work fine now
I integrat program to be GUI using Tkinter and insted search in curent direction I try to be from browser
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 < 15:
-
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()
but give me this error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "C:\Documen ts and Settings\Admini strator\Desktop \ICS482\hw3\pro gramAli.py", line 21, in most_frequant_w ord
for root, dirs, files in os.walk(browser ):
File "C:\Python25\li b\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
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 - 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: Paul |
last post by:
I am creating a Program for college, in which the Program will read a Folder
and create
a HTML page from the pictures that are storrd in that folder. .
What would be the best way to do it in VB Net 2003.
Thanks
Paul Selwood
|
by: Paul |
last post by:
I am creating a Program for college, in which the Program will read a Folder
and create a HTML page from the pictures that are storrd in that folder.
..
What would be the best way to do it in VB Net 2003.
Thanks
Paul Selwood
Paul@DialUpSelwood.demon.co.uk
|
by: Davie |
last post by:
I am new to .Net, so apologies if this is a simple question.
I need a way of display folders and files to my users. However, it must
show whether they have NTFS access to the file/folder.
For example, if they have access the folder or file is displayed as a link,
if they dont it is displayed only as text.
Is this possible? Any help would be greatly appreciated.
|
by: tonelab |
last post by:
I have an aspx page that does not have a separate source for the VB - it is
on top in between the <script> tags. I use the following statement
Dim oComLib as New ComLib
To reference a class called ComLib in a source member called ComLib.vb that
is found in the /Code folder of my project. It all works fine in the VWD2005
environment as when I run the aspx page, it processes the above statment and
"sees" the class reference in the code.
|
by: eholz1 |
last post by:
Hello PHP Group,
I am having trouble setting permissions correctly so that the
magickwand api (php 5.2) can read and
write images. I usually read a file from one directory, create a
magickwand resource from that file,
and transform the image, and save the new image with a new name to a
different directory.
I have seen that my file and folder permissions when set incorrectly,
| |
by: =?Utf-8?B?SmVmZnJleQ==?= |
last post by:
I made a typo on a Project name (e.g. Wong, instead of Wang). Later on I
renamed the Soution, Project, WebForm., etc, except the file folder name,
back to Wang. Then after I closed the VS.net and went back to the Windows
Explorer, I found out the file folder name was Wong, so I changed it to
Wang. Next time, when I tried to open the Wang project, an error message
showed that VS can not open the Wong Web page (something like that).
...
|
by: kai |
last post by:
Hi, All
I am trying to create a file folder for any login user, and create sub
folders for the user on a web page. After the user login again, he can only
sees his own folder on the Web page. I am planning to use VB2005 or C#.
I look the help files and searched on the web, I cannot find the answer. Is
this possble in ASP.NET 2.0?
Thanks
|
by: cbz9633 |
last post by:
hi
i want to read a folder name, after that read folder names within that folder and at last the file name and access it, that i know but i dont know how to read a folder name.
please help
|
by: parshupooja |
last post by:
Contact Reply
1 point Member
propoo
Joined on 08-31-2007, 10:32 PM
Posts 3
Hey all ,
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |