find_files.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:geekbook 作者: mmagnus 项目源码 文件源码
def file_search(filename, verbose):
    """Search for filename. Returns dirname of the filename's path, and the full path.

    170107 add cache. If the db is not found, create an empty pandas df 
    and populate this df with append later. If the filename is not in the db
    run g/locate. Then, save the found path to the db (using pandas, via df, to json)"""

    # cache
    if os.path.isfile(JSON_DB):
        df = pd.read_json(JSON_DB, orient='records')
        #filename = 'x.pse'
        pathdf = df[df['fn'] == filename]['path']
        if not pathdf.empty:
            path = pathdf.to_string(index=False)
            logger.info('find file [from the db]:' + filename)
            return os.path.dirname(path), path
    else:
        df = pd.DataFrame()

    # if filename is not found in the db
    logger.info('find file:' + filename)

    if platform.system() == "Linux":
        out = commands.getoutput('locate ' + filename)
    if platform.system() == "Darwin":
        out = commands.getoutput('glocate ' + filename)
    first_hit = out.split('\n')[0]
    logger.info('# of hits ' + str(len(out.split('\n'))) + " " + out.replace('\n',', '))
    if not first_hit:
        logger.info('not found')
    else:
        logger.info('hit ' + first_hit)

    # update cache
    dffile = pd.DataFrame([[filename, first_hit],], columns=['fn', 'path'])
    df = df.append(dffile, ignore_index=True)
    # save to json
    df.to_json(JSON_DB, orient='records')
    ##
    return os.path.dirname(first_hit), first_hit
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号