Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 12th, 2006, 12:12 AM   #1
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
Searching for a file with Python

Hi, all, I'm trying to work on a file search implemented in Python... basically located an arbitrary file. Trying to figure out exactly what I'm doing wrong... it's a recursive method, but I can't get it to stop "calling" itself after it's found the file.

Also, I may be reinventing the wheel here, as this takes forever and a day to search, while i can use the command line "locate file" for much quicker access. Is there Python module to search for a file's location? I haven't been able to find one. :/ Not really asking for anyone to write it for me, but if there is a way I can access the same data that "locate" uses using Python, a gentle push in the right direction would be handy.

Anyway, here's my code... any comments or criticisms on my method are more than welcome.

import os

class filesearch:
	def __init__(self, file, directory):
		self.file = file
		self.directory = directory
		self.ignoredDir = ["sys","dev","tmp","proc"]
	def fileInDir(self):
		try: 
			if os.path.exists(self.directory+"/"+self.file):
				print "Found %s" % self.directory+"/"+self.file
				
			else:
				print "%s does not exist!" % (self.directory+"/"+self.file)
				dirlist = []
				for i in os.listdir(self.directory):
					if os.path.isdir(self.directory+"/"+i):
						if i not in self.ignoredDir and not os.path.islink(self.directory+"/"+i):
							dirlist.append( self.directory+"/"+i )
				for i in dirlist:
					
						f = filesearch(self.file, i)
						f.fileInDir()
		except OSError: 
			print "Could not access %s" % (self.directory)
darthsabbath is offline   Reply With Quote
Old Jan 12th, 2006, 3:11 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You need to have some sort of limiting condition:
def file_search(search_file, path):
    for filename in os.listdir(path):
        full_filename = os.path.join(path, filename)

        if filename == search_file:
            return full_filename

        if os.path.isdir(full_filename) and not os.path.islink(full_filename):
            found_file = file_search(search_file, full_filename)
            if found_file != None:
                return found_file

    return None
Once the file is found, the filename is returned. If a file_search instance detects that one of its children has received a filename that is not None, it returns also, thus breaking the recursion.
Arevos is offline   Reply With Quote
Old Jan 12th, 2006, 11:47 AM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Hey, it would be good to use os.walk if you don't know what folder the file is located in.

import os

for path,files,dirs in os.walk('C:/'):


Something like that. :banana:
Sane is offline   Reply With Quote
Old Jan 12th, 2006, 1:10 PM   #4
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
Awesome, thanks guys, I appreciate it!

Phil
darthsabbath is offline   Reply With Quote
Old Jan 12th, 2006, 4:20 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Sane
Hey, it would be good to use os.walk if you don't know what folder the file is located in.
Oho! I didn't know about that particular function. So my previous search function could be very neatly reduced to a mere four lines:
def file_search(search_file, path):
    for path, dirs, files in os.walk(path):
        if search_file in files:
            return os.path.join(path, search_file)
Arevos is offline   Reply With Quote
Old Jan 12th, 2006, 4:46 PM   #6
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
Swank!

Shows I definitely need to start thinking things through a little better. ;-)
darthsabbath is offline   Reply With Quote
Old Jan 13th, 2006, 12:59 PM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Google for 'Python path module' if you're gonna do a lot of path related manipulation - you may find it to your liking. The module provides a nicer object orientated abstraction around the default functions - it makes for nicer code and is a single file so it can be easily included in your project.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC