Simulate windows full-fledged search

Source: Internet
Author: User

Iterate through folders on your PC, save to MySQL database, search, and fetch data from database. 
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 ') /c0>
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):
# Name
file_name = Os.path.basename (NewPath)
# type
File_type = Os.path.splitext (NewPath) [1]
# Full path
File_path = NewPath
# size
file_size = os.path.getsize (NewPath)
# last access time
last_time = Datetime.datetime.fromtimestamp (Os.path.getatime (NewPath))
# creation Time
create_time = Datetime.datetime.fromtimestamp (Os.path.getctime (NewPath))
# modification Time
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, ' filename ')
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 ') /c2>
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= ' the ")
# Thread.Start ()
# thread.join ()
file_name = input (' Enter filename for search: ')
Thread1 = Threading. Thread (Target=search_file, args= (file_name, conn, cursor), name= ' Search.one ')
Thread1.start ()
# Search_file (file_name, conn, cursor)

Simulate windows full-fledged search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.