![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 15
Rep Power: 0
![]() |
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) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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: |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2005
Posts: 15
Rep Power: 0
![]() |
Awesome, thanks guys, I appreciate it!
Phil |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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) |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2005
Posts: 15
Rep Power: 0
![]() |
Swank!
Shows I definitely need to start thinking things through a little better. ;-) |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|