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