--- Title: pythonでfoobarのalternativeを作る 4 Author: yamasyuh68 Web: https://mimemo.io/m/xn7vg4W5BroAa1q --- 190510 ## ファイル列挙、DB作成の高速化 - 昨日は飲みに行って帰ったら落ちた(-_-) ファイル列挙を高速に出来ないか Everythingはどうやってるんじゃろ? - ***まずはいろいろな時間を計ってみよう***(1万ファイルくらいがいいかな) ・globのみの時間 ・チェックカラム更新に要する時間 コンピューター:C言語講座:ディレクトリ内容の読み出し http://www.ncad.co.jp/~komata/c-kouza14.htm ----- update staff set name = 'Nakajima' where id = 3 ①作成 CREATE TABLE musics(fullpath PRIMARY KEY, mdata ,title,artist,alubumartist,alubum,date,check); ②更新チェック チェックカラムをクリア update musics set check = 0 # no where --> all p # iterate filename os.path.getmdata(p) SELECT fullpath ,mdata FROM musics WHERE fullpath=p if fetchone() : 取り出せれば mdataを比較 同じならパス               違ったらUPDAte else 取り出せなければインサート チェックを1に更新 最後にチェック0があればデータ削除 count={'all':0 , 'dir':0 , 'mfile':0 , 'ofile':0} count['all']+=1 ``` import glob ,re , os import sqlite3 import time # ファイルの更新日時に変更等があればチェック # データベース作成済み(ファイル名、mtime のみ) count={'all':0 , 'file':0 , 'same':0 , 'diff':0 ,'none':0 , 'new':0} conn = sqlite3.connect( ) c = conn.cursor() c.execute('create table IF NOT EXISTS musics(fullpath PRIMARY KEY, mtime ,title,alubum)') fo=r'i:\Music\jazz\**' sel='select mtime from musics where fullpath = ?' start = time.time() for p in glob.iglob(fo , recursive=True): count['all']+=1 if os.path.isfile(p): count['file']+=1 nmtime=os.path.getmtime(p) c.execute(sel,(p,)) res=c.fetchone() if res : if res[0]==nmtime : count['same']+=1 else : count['diff']+=1 print(p) else: count['new']+=1 print(p) elapsed_time = time.time() - start print ("elapsed_time:{0}".format(elapsed_time) + "[sec]") c.execute('select count (*) from musics') print('data--{0}'.format(c.fetchall())) print(count) conn.close() ```