"Locate" command in Python

M

mwt

Is there a function in python that does what "locate" does in a bash
shell?

I know I could do it by using os.popen('locate'), but I'm curious if
there's a Python "native" way to go about it. Only needs to work in
Unix, but would be interesting if it was cross-platform.

Thanks.
 
A

Adonis

mwt said:
Is there a function in python that does what "locate" does in a bash
shell?

I know I could do it by using os.popen('locate'), but I'm curious if
there's a Python "native" way to go about it. Only needs to work in
Unix, but would be interesting if it was cross-platform.

Thanks.

Here is a quick hack I just did, its very ugly, but does the job.

First do locate -u to create a cache then just locate [term], its not so
fancy as to remind you when the cache is too old, but hey.

It requires Python 2.3+

Hope this helps.

Adonis


---

import os
import sys

rootPath = "/"

def search(term):
if os.path.exists("files.cache"):
cache = file("files.cache", 'r')
for line in cache:
if term in line:
print line.strip()
cache.close()
else:
print "Please update the cache"

def cache():
cache = file("files.cache", 'w')
for root, dirs, files in os.walk(rootPath):
for aDir in dirs:
cache.write("%s\n" % aDir)
for aFile in files:
filePath = os.path.join(root, aFile)
filePath = os.path.normpath(filePath)
cache.write("%s\n" % filePath)
cache.close()

if __name__ == "__main__":
try:
if sys.argv[1] == "-u":
cache()
else:
search(sys.argv[1])
except IndexError:
print "Usage: locate [-u] [term]"
 
B

BartlebyScrivener

How about one of these that works on Windows XP? I know there's no
files.cache, but I wonder if your script could be combined with another
function that would generate a list of paths on a Windows XP machine.

Anyway, thanks for the script.
 
A

Adonis

BartlebyScrivener said:
How about one of these that works on Windows XP? I know there's no
files.cache, but I wonder if your script could be combined with another
function that would generate a list of paths on a Windows XP machine.

Anyway, thanks for the script.

I wrote it on a Windows XP machine. The files.cache is generated when
you use the -u option. For example if you saved the script as locate.py
first at a command prompt: python locate.py -u this will create the
files.cache, it simply walks through your entire hard drive writing all
the directories and files it finds along the way. Then doing: python
locate.py SomeFileOrDirName will go through the files.cache matching
whatever term your looking for if present. Although the rootPath
variable is set to the POSIX style path of "/" Python converts it to a
proper path. At least thats my assumption as it works fine on my system.

Adonis
 
B

BartlebyScrivener

This script is COOL. It should be in the next cookbook. Maybe with some
tweaks and switches.

Thanks again.

Rick
 
M

mwt

On my system, files.cache ended up being 45.9 Mb (800,000+ lines)!
Pretty fun script. I can imagine some interesting variations.
Thanks!
 
B

BartlebyScrivener

45.9 mb

Yikes.

I keep all of my data files on a separate logical drive. I indexed only
that one. I'm going to try and figure a way to store the results of
os.walk(root) as a shelve, and then search it that way.

In the meantime, you might try the script we were playing with in the
previous thread.
It's from the Python Cookbook. It doesn't create a file for searching,
but gets the job done.

http://groups.google.com/group/comp.lang.python/browse_frm/thread/90e36791afbb90b8

I'm thinking there is a way to crossbreed this script with Adonis's for
something lightning fast.

I'll post it if I get free time to make it.

rpd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top