類比windows全盤搜尋

來源:互聯網
上載者:User

標籤:err   utf8   dir   where   ase   tin   mysql資料庫   style   類比   

迴圈遍曆pc上的檔案夾,儲存到mysql資料庫中,搜尋時,從資料庫取資料。
import os
import datetime
import pymysql
import threading


def link_db():
conn = pymysql.connect(host=‘localhost‘, port=3306, user=‘root‘, password=‘123456‘, db=‘win‘, charset=‘utf8mb4‘)
cursor = conn.cursor()
return cursor


def save_file(path, conn, cursor):

try:
print(path)
dirList = os.listdir(path)
for i in dirList:
if i.startswith(‘.‘):
continue
newPath = os.path.join(path, i)
if os.path.isdir(newPath):
save_file(newPath, conn, cursor)
elif os.path.isfile(newPath):
# 名稱
file_name = os.path.basename(newPath)
# 類型
file_type = os.path.splitext(newPath)[1]
# 完整路徑
file_path = newPath
# 大小
file_size = os.path.getsize(newPath)
# 上次訪問時間
last_time = datetime.datetime.fromtimestamp(os.path.getatime(newPath))
# 建立時間
create_time = datetime.datetime.fromtimestamp(os.path.getctime(newPath))
# 修改時間
update_time = datetime.datetime.fromtimestamp(os.path.getmtime(newPath))
sql = "insert into `files`(`file_name`, `file_type`, `file_path`, `file_size`, `last_time`, " \
"`create_time`, `update_time`) values(%s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (file_name, file_type, file_path, file_size, last_time, create_time, update_time))
conn.commit()
print(file_name , ‘檔案名稱‘)
except FileNotFoundError as e:
pass
except PermissionError as e:
pass

def search_file(name, conn, cursor):
sql = ‘select file_name, file_path from files where file_name like "%{}%" ; ‘.format(name)
cursor.execute(sql)
data = cursor.fetchall()
for file in data:
print(file)



if __name__ == ‘__main__‘:
conn = pymysql.connect(host=‘localhost‘, port=3306, user=‘root‘, password=‘123456‘, db=‘win‘, charset=‘utf8mb4‘)
cursor = conn.cursor()
# sql = ‘delete from files;‘
# cursor.execute(sql)
# conn.commit()
# path = ‘D:\\‘
# thread = threading.Thread(target=save_file, args=(path, conn, cursor), name=‘No.1‘)
# thread.start()
# thread.join()
file_name = input(‘輸入搜尋的檔案名稱:‘)
thread1 = threading.Thread(target=search_file, args=(file_name, conn, cursor), name=‘search.one‘)
thread1.start()
# search_file(file_name, conn, cursor)

類比windows全盤搜尋

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.