[原创]找到文件的出处

Python/PHP/Perl 开发与设计
回复
baic
帖子: 166
注册时间: 2006-08-22 23:10
送出感谢: 0
接收感谢: 0

[原创]找到文件的出处

#1

帖子 baic » 2007-01-11 14:42

代码: 全选

#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
#文件名search.py
#使用方法(如果没有建立过数据库请运行./search.py)下一行是搜索文件所在数据包例子
#find /etc/cron.* -type f| xargs ./search.py
#find /etc/ -type f| xargs ./search.py |grep "找不到"

import popen2
import os,dbhash,sys
f=popen2.popen2
def get():
    print "创建系统文件包和文件对应关系数据库","systemfilesearch.dbcache"
    read,write=f("dpkg -l |awk '{print $2}'")
    write.close()
    read.readline();read.readline();read.readline();read.readline();read.readline()
    d=read.readline()[:-1];tmp=[]
    while d:
        tmp.append(d);d=read.readline()[:-1]
    read.close()

    db=dbhash.open("systemfilesearch.dbcache","c")
    for i in tmp:
        print "搜索文件包",i
        read,write=f("dpkg -L "+i)
        write.close()
        d=read.readline()[:-1]
        while d:
            if os.path.isfile(d):db[d]=i              
            d=read.readline()[:-1]
        read.close()
    db.close()

if __name__=="__main__":
    if len(sys.argv)==1:get()
    else:
        db=dbhash.open("systemfilesearch.dbcache","c")
        for i in sys.argv[1:]:print i,"文件所在包为>    ",db.get(i,"找不到")
        db.close()

回复

回到 “Python/Php/Perl”