473,789 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

17 New Member
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

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. 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
Mar 18 '08 #1
11 2134
jlm699
314 Contributor
Is there way to enter name of parent folder(cars) and search in all sub folder(Toyota,H onda and BMW) and files ?
I'm sorry, but it's very difficult to understand what it is that you are asking. I can provide you with some direction however...

Perhaps something you're looking for is os.walk. Here is a sample:
Expand|Select|Wrap|Line Numbers
  1. >>> for root, dirs, files in os.walk(os.getcwd()):
  2. ...     print 'Looking into %s' % root.split('\\')[-1]
  3. ...     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  4. ...     for idx, dir in enumerate(dirs):
  5. ...         print 'Directory #%d: %s' % (idx + 1, dir)
  6. ...     for idx, file in enumerate(files):
  7. ...         print 'File #%d: %s' % (idx + 1, file)
  8. ...     
  9. Looking into pythtests
  10. Found 2 dirs and 16 files
  11. Directory #1: graphics
  12. Directory #2: Question
  13. File #1: bckmch.py
  14. File #2: cmdtest.py
  15. File #3: cobyla.py
  16. File #4: elseerr.py
  17. File #5: fileio.py
  18. File #6: ldict.py
  19. File #7: lid
  20. File #8: mainbody
  21. File #9: matrixprint.py
  22. File #10: matrx_print.py
  23. File #11: test.py
  24. File #12: test2.py
  25. File #13: topload
  26. File #14: totalbottle
  27. File #15: trivgame.py
  28. File #16: wxtemplate.py
  29. Looking into graphics
  30. Found 0 dirs and 8 files
  31. File #1: Buttons.py
  32. File #2: dice_class.py
  33. File #3: ghostchars.py
  34. File #4: graphics.py
  35. File #5: graphics.pyc
  36. File #6: graphics22.py
  37. File #7: graphics22.pyc
  38. File #8: hw6-template.py
  39. Looking into Question
  40. Found 0 dirs and 0 files
  41. >>> 
Hope that helps a little bit
Mar 18 '08 #2
alivip
17 New Member
I mean example of parent dirctory (folder) is cars and example of subdirectory (folder) is (BMW,Honda,Toyo ta) so I wont to trace directory and all subdirctory
to find most frequant word in all text files (together) under specific folder


and I did not understand what your code mean
Mar 19 '08 #3
alivip
17 New Member
thanx M.r jlm699 your reply was helpfull

but it does not match what I wont exactly

modifyig code is

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. import os.path
  10. for root, dirs, files in os.walk(os.getcwd()):
  11.   print 'Looking into %s' % root.split('\\')[-1]
  12.   print 'Found %d dirs and %d files' % (len(dirs), len(files))
  13.   for idx, dir in enumerate(dirs):
  14.     print 'Directory #%d: %s' % (idx + 1, dir)
  15.     for idx, file in enumerate(files):
  16.       print 'File #%d: %s' % (idx + 1, file)
  17.       ff = open (file, "r")
  18.       text = ff.read ( )
  19.       ff.close ( )
  20.  
  21.       word_freq = {}
  22.  
  23.       word_list = text.split()
  24.  
  25.       for word in word_list:
  26.         # word all lower case
  27.          word = word.lower()
  28.         # strip any trailing period or comma
  29.          word = word.rstrip('.,/"-_;\[]()')
  30.         # build the dictionary
  31.          count = word_freq.get(word, 0)
  32.          word_freq[word] = count + 1
  33.  
  34.     # create a list of (freq, word) tuples
  35.       freq_list = [(freq, word) for word, freq in word_freq.items()]
  36.  
  37.     # sort the list by the first element in each tuple (default)
  38.       freq_list.sort(reverse=True)
  39.  
  40.       for n, tup in enumerate(freq_list):
  41.         # print the first ten items
  42.          if n < 10:
  43.             freq, word = tup
  44.             print freq, word
  45.  
the output like

File #12: listtoDict.py
14 with
6 python
6 for
File #13: parseAddresses
3 python
1 with
1 will
and I need to find frequacy of word in all text file not seperat for examle the previos output shud be like

15 with
9 python
6 for
1 will

so add frequancy of word in (File #12: listtoDict.py) with (File #13: parseAddresses) and print thim in one list
Mar 19 '08 #4
jlm699
314 Contributor
and I need to find frequacy of word in all text file
Just move your word_freq dictionary declaration to before you begin the for loop, and then move the sorting/printing of that structure to after the for loop. And you'll achieve this.
Mar 19 '08 #5
jlm699
314 Contributor
Here's the modifications that I suggest above and the resulting output.
Expand|Select|Wrap|Line Numbers
  1. import sys, os
  2.  
  3. word_freq = {}
  4.  
  5. for root, dirs, files in os.walk(os.getcwd()):
  6.     print 'Looking into %s' % root.split('\\')[-1]
  7.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  8.  
  9.     for idx, file in enumerate(files):
  10.         ff = open (os.path.join(root, file), "r")
  11.         text = ff.read ( )
  12.         ff.close ( )
  13.  
  14.     word_list = text.strip().split()
  15.  
  16.     for word in word_list:
  17.         word = word.lower().rstrip('.,/"-_;\\[]()')
  18.         if word.isalpha():
  19.             # build the dictionary
  20.             count = word_freq.get(word, 0)
  21.             word_freq[word] = count + 1
  22.  
  23.     # create a list of (freq, word) tuples
  24.     freq_list = [(freq, word) for word, freq in word_freq.items()]
  25.  
  26.     # sort the list by the first element in each tuple (default)
  27.     freq_list.sort(reverse=True)
  28.  
  29. for n, tup in enumerate(freq_list):
  30.     # print the first ten items
  31.     if n < 10:
  32.         freq, word = tup
  33.         print freq, word
Output:
Expand|Select|Wrap|Line Numbers
  1. Microsoft Windows XP [Version 5.1.2600]
  2. (C) Copyright 1985-2001 Microsoft Corp.
  3.  
  4. C:\Documents and Settings\Administrator>cd Desktop\pythtests
  5.  
  6. C:\Documents and Settings\Administrator\Desktop\pythtests>python walkncount.py
  7. Looking into pythtests
  8. Found 2 dirs and 17 files
  9. Looking into graphics
  10. Found 0 dirs and 8 files
  11. Looking into Question
  12. Found 0 dirs and 0 files
  13. 46 the
  14. 17 and
  15. 14 of
  16. 14 a
  17. 12 is
  18. 10 to
  19. 10 in
  20. 8 you
  21. 8 this
  22. 8 that
  23.  
  24. C:\Documents and Settings\Administrator\Desktop\pythtests>
Mar 19 '08 #6
alivip
17 New Member
thanx alot
but it is actualy read all file but print frequancy of only one of them
not print frequancy of word in all file which I wont
Mar 19 '08 #7
jlm699
314 Contributor
read all file but print frequancy of only one of them
Ok... I'm not sure exactly what you mean by that but I think that you're trying to say you only want to display the frequency of words in the file with the highest frequencies?

Expand|Select|Wrap|Line Numbers
  1. import sys, os
  2.  
  3. highest_freq = [(0,'Blank')]
  4. high_file_name = ''
  5.  
  6. for root, dirs, files in os.walk(os.getcwd()):
  7.     # print 'Looking into %s' % root.split('\\')[-1]
  8.     # print 'Found %d dirs and %d files' % (len(dirs), len(files))
  9.  
  10.     for idx, file in enumerate(files):
  11.         # print 'File #%d: %s' % (idx + 1, file)
  12.         ff = open (os.path.join(root, file), "r")
  13.         text = ff.read ( )
  14.         ff.close ( )
  15.  
  16.         word_freq = {}
  17.         word_list = text.strip().split()
  18.  
  19.         for word in word_list:
  20.             word = word.lower().rstrip('.,/"-_;\\[]()')
  21.             if word.isalpha():
  22.                 # build the dictionary
  23.                 word_freq[word] = word_freq.get(word, 0) + 1
  24.  
  25.         # create a list of (freq, word) tuples
  26.         freq_list = [(freq, word) for word, freq in word_freq.items()]
  27.  
  28.         # sort the list by the first element in each tuple (default)
  29.         freq_list.sort(reverse=True)
  30.         if freq_list:
  31.             if freq_list[0][0] > highest_freq[0][0]:
  32.                 highest_freq = freq_list
  33.                 high_file_name = file
  34.  
  35. print 'Highest frequency file: %s' % high_file_name
  36. for n, tup in enumerate(highest_freq):
  37.     if n < 10:
  38.         freq, word = tup
  39.         print freq, word
  40. raw_input('\nHit enter to exit')
  41.  
Output:
Expand|Select|Wrap|Line Numbers
  1. Highest frequency file: graphics.py
  2. 93 def
  3. 44 return
  4. 36 the
  5. 31 in
  6. 29 of
  7. 26 for
  8. 25 if
  9. 23 to
  10. 19 class
  11. 19 a
  12.  
  13. Hit enter to exit
  14.  
This is a crude example so I apologize; however I don't understand what you're trying to do or why. So working with what you've given this is the most I can make of your question.
Mar 19 '08 #8
alivip
17 New Member
realy I aprechat your trying to help
but unfortionatly that is no wat I ment

I meat is read all files in directory compin all words in all files and put them in new file then find freqancy of each word in taht new file
Mar 20 '08 #9
jlm699
314 Contributor
realy I aprechat your trying to help
but unfortionatly that is no wat I ment

I meat is read all files in directory compin all words in all files and put them in new file then find freqancy of each word in taht new file
So basically, you're saying you want to combine the contents of all the files into a new file, and then find the frequency of the words in that file?

Well to do that w/o creating a new file would be a very slight change from a previous post:
Expand|Select|Wrap|Line Numbers
  1. import sys, os
  2.  
  3. word_freq = {}
  4.  
  5. for root, dirs, files in os.walk(os.getcwd()):
  6.     print 'Looking into %s' % root.split('\\')[-1]
  7.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  8.  
  9.     for idx, file in enumerate(files):
  10.         ff = open (os.path.join(root, file), "r")
  11.         text = ff.read ( )
  12.         ff.close ( )
  13.  
  14.         word_list = text.strip().split()
  15.  
  16.         for word in word_list:
  17.             word = word.lower().rstrip('.,/"-_;\\[]()')
  18.             if word.isalpha():
  19.                 # build the dictionary
  20.                 count = word_freq.get(word, 0)
  21.                 word_freq[word] = count + 1
  22.  
  23.     # create a list of (freq, word) tuples
  24.     freq_list = [(freq, word) for word, freq in word_freq.items()]
  25.  
  26.     # sort the list by the first element in each tuple (default)
  27.     freq_list.sort(reverse=True)
  28.  
  29. for n, tup in enumerate(freq_list):
  30.     # print the first ten items
  31.     if n < 10:
  32.         print "%s times: %s" % tup
  33. raw_input('\nHit enter to exit')
  34.  
Mar 20 '08 #10

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

Similar topics

2
2888
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
0
1490
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
3
1280
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.
7
1272
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.
3
8282
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,
2
2270
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). ...
10
14305
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
1
2516
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
0
2317
parshupooja
by: parshupooja | last post by:
Contact Reply 1 point Member propoo Joined on 08-31-2007, 10:32 PM Posts 3 Hey all ,
0
9663
marktang
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...
0
9506
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,...
0
10404
Oralloy
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...
0
10193
jinu1996
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...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5415
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...
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.