AdrienLF
10/29/2017 - 10:07 AM

Two simple python scripts to make a database of all files starting from the script folder, and another one to search the database.

Two simple python scripts to make a database of all files starting from the script folder, and another one to search the database.

#!/usr/bin/env python3

import os
import apsw

from pathlib import Path

rootPath = os.getcwd()


def convert_bytes(num):
    """
    this function will convert bytes to MB.... GB... etc
    """
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)
        num /= 1024.0


###
### Opening/creating database
###
dbname = str(rootPath).replace(os.sep, '_') + "-file_list"
print(dbname)
connection=apsw.Connection(dbname)
cursor=connection.cursor()

cursor.execute("CREATE TABLE IF NOT EXISTS File_list (id INTEGER PRIMARY KEY ASC, file_name text, size_in_byte INTEGER, size TEXT)")
cursor.execute("DELETE FROM File_list")

try:
    for subdir, dirs, files in os.walk(rootPath):
        for file in files:
            file_Full_Path = os.path.join(subdir, file)
            try:
                file_Size = Path(file_Full_Path).stat().st_size
            except Exception as e:
                print('File size error')
                print(e)
            #print(file_Full_Path)
            #print(convert_bytes(file_Size))
            cursor.execute("INSERT INTO File_list (file_name, size_in_byte, size) VALUES(?,?,?)", (file_Full_Path, int(file_Size), convert_bytes(file_Size)))

    for row in cursor.execute("SELECT * FROM File_list"):
        print(row)
except Exception as e:
    print(e)
    pass
#!/usr/bin/env python
import os
import apsw

name=input("What file are you looking for? \n")

search='%'+name+'%'
print("\n"+"Looking for " + name + '\n')

rootPath = os.getcwd()

def find_database():
    for subdir, dirs, files in os.walk(rootPath):
        for file in files:
            if "-file_list" in file:
                print("Database found! Using "+ file + '\n')
                return file


connection=apsw.Connection(find_database())
cursor=connection.cursor()

for row in cursor.execute("SELECT file_name, size FROM File_list WHERE file_name LIKE ? ORDER BY size_in_byte DESC ", (search,)):
    print(row)