Welcome, guest | Sign In | My Account | Store | Cart

This recipe is "updatedb" like command.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys
import os
import platform
import sqlite3

locatedb = os.path.join(os.path.expanduser('~'), 'sqlite3-locate.db')
if os.path.exists(locatedb):
    os.remove(locatedb)

def wholepath():
    pathes = []
    if 'Windows' == platform.system():
        for drv in [chr(d) for d in range(ord('A'), ord('Z')+1)]:
            root = drv + r':' + os.sep
            if os.path.exists(root):
                pathes.append(root)
        return pathes
    return [os.sep]

def mktarget(*targets):
    if len(targets) == 0:
        return wholepath()
    return targets

db = sqlite3.connect(locatedb)
db.text_factory=str
cur = db.cursor()
cur.execute('CREATE TABLE locatedb(path TEXT PRIMARY KEY)')

for target in mktarget(*sys.argv[1:]):
    for root, dirs, files in os.walk(target):
        for fn in files:
            cur.execute('INSERT INTO locatedb VALUES(?)', (os.path.join(root, fn),))
db.commit()