469,915 Members | 2,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,915 developers. It's quick & easy.

"Locate" command in Python

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.

Apr 10 '06 #1
7 10092
mwt wrote:
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]"
Apr 10 '06 #2
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.

Apr 10 '06 #3
BartlebyScrivener wrote:
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
Apr 10 '06 #4
Adonis,

Cool! I'm on it.

Thanks again.

rpd

Apr 10 '06 #5
This script is COOL. It should be in the next cookbook. Maybe with some
tweaks and switches.

Thanks again.

Rick

Apr 10 '06 #6
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!

Apr 10 '06 #7
>>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....e36791afbb90b8

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

Apr 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Frederick Gotham | last post: by
14 posts views Thread by Nader Emami | last post: by
3 posts views Thread by dwblas | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.