码迷,mamicode.com
首页 > Windows程序 > 详细

模拟windows全盘搜索

时间:2018-05-21 19:50:48      阅读:258      评论:0      收藏:0      [点我收藏+]

标签: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全盘搜索

标签:err   utf8   dir   where   ase   tin   mysql数据库   style   模拟   

原文地址:https://www.cnblogs.com/changqing8023/p/9068446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!