|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Searching sub directories in a path
Greets,
I managed to use bits of code that I found the boards to make a simple search tool to help me determine if I already have a given file(mp3's,flash movies,etc).My problem is searching within sub directories in the path.I can identify the subs with the use of "isdir",but I was wondering how to also search within those subs,and display the results.Also,I was wondering if there was a way to ignore the text case,instead of converting the text to all upper/lower case.I tried using the re module and ignorecase,but I kept getting errors when I ran it.Here's the code: Code:
import os
exit = ('quit', 'exit', 'q', 'e', '0')
while True:
try:
path = raw_input('\nDirectory to search: ')
path2 = path.lower()
if path2 in (exit):
print 'Good Bye'
break
string = raw_input('String to search for: ')
string2 = string.lower()
if string2 in (exit):
print 'Good Bye'
break
ext = raw_input('Specify the extension of files to examine: ')
ext2 = ext.lower()
if ext2 in (exit):
print 'Good Bye'
break
count = 0 #total number of objects
count1 = 0 #number of files
count2 = 0 #files ending with the extension
direct = 0 #number of sub-directories in path
found = []
subs = []
for file in os.listdir(path):
count = count+1
if os.path.isdir(path+file):
direct = direct+1
subs.append(file)
if os.path.isfile(path+file):
count1 = count1+1
file2 = file.lower()
if file2.endswith(ext2):
count2 = count2+1
if string2 in (path+file2) and file2.endswith(ext2):
found.append(file)
print '\nTotal Objects In Directory: ', count
print 'Number Of Sub Directories:', direct
print 'Number Of Files:', count1
print 'Number Of Files Ending With "'+ext+'": ', count2
print '\nSub Directories:'
#print subs
for file in subs:
print file
print '\n"'+ext+'" files that have "'+string+'" in the name:'
#print found
for file in found:
print file
except:
print '\nError.Try again.'
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!? |
|
#2
|
|||
|
|||
|
you want to check out the os.walk() function. it traverses through the directory tree, starting at a root directory that you give it, and applying a function (that you specify) to each directory it comes across. that's just from memory, so you'll need to check out the os module in the library documentation.
as for the converting to upper/lower case, that's probably the easiest way to do it. however, you don't need two variables for each conversion. if you are on a platform that is case insensitive (Windows), then you don't need to keep the original string and can do: path = raw_input("Path: ").lower() however, if your on a case sensitive platform, just use the lower() function in your if statements: Code:
path = raw_input("Path: ")
if path.lower() in exit:
...
do stuff
...
hope that helped a bit. the documentation on the walk() function is at the very bottom of this page: http://www.python.org/doc/current/lib/os-file-dir.html |
|
#3
|
||||
|
||||
|
Thank you very much rebbit.The os.walk() seems to be exactly what I'm looking for.But since I am by no means a programmer,I was a little confused by one of the notes(which also explains why I kept the converted text as a new variable).Could anyone please explain to me(in knuckle dragger terms) what this note means:
Quote:
I am using Windows XP with Python 2.3.Thank you in advance for your patience and help. |
|
#4
|
|||
|
|||
|
You should also definitely check out Jason Orendorff's path module. It's a much more comfortable way of working with paths.
What the documentation means when it says that the os.walk function never changes current working directory is that the function returns path names which are relative to the same directory. for instance, "local", "include", "local/bin", "share/doc" are all paths relative to the "/usr" directory if the joined paths exist , like: "/usr/local", "/usr/include", etc. If you were to change directory in the middle of a traversal this means that you would get paths relative to a path other than your current working directory. This is not good. Last edited by percivall : May 2nd, 2004 at 03:24 AM. |
|
#5
|
||||
|
||||
|
Ahhh,ok thank you for the explanation percivall.But the link you posted seems to be broken.No biggie though,I'm sure I'll be able to find the info via some searching,seeing how you provided Mr.Orendorff's name.Thanks again!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Searching sub directories in a path |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|