Current Path : /var/www/html/clients/ |
Current File : /var/www/html/clients/godir_3.py |
import os import time import sqlite3 from datetime import datetime def create_database(): conn = sqlite3.connect('files.db') cursor = conn.cursor() cursor.execute('''CREATE TABLE IF NOT EXISTS files (path TEXT, name TEXT, content TEXT, cdate TEXT,mdate TEXT)''') conn.commit() conn.close() def get_php_txt_files(directory): php_txt_files = [] for root, _, files in os.walk(directory): for file in files: os.system('clear') print('Prepare list of txt/php files') print(root,file) if file.endswith('.php') or file.endswith('.txt') or file.endswith('.html'): php_txt_files.append(os.path.join(root, file)) return php_txt_files def process_files(files): print('Writing...') conn = sqlite3.connect('files.db') cursor = conn.cursor() for file_path in files: start_time = time.time() try: with open(file_path, 'r') as f: for line in f: if line.strip(): ctime = os.path.getctime(file_path) mtime = os.path.getmtime(file_path) ctime_obj = datetime.fromtimestamp(ctime) mtime_obj = datetime.fromtimestamp(mtime) cursor.execute('''INSERT INTO files (path, name, content, cdate, mdate) VALUES (?, ?, ?, ?, ?)''', (file_path, os.path.basename(file_path), line.strip(), ctime_obj.strftime('%Y-%m-%d %H:%M:%S'), mtime_obj.strftime('%Y-%m-%d %H:%M:%S'))) except Exception as e: print('Error processing '+file_path+': '+str(e)) execution_time = time.time() - start_time os.system('clear') print('Writing - '+file_path+'\nProccess - '+str((files.index(file_path)+1)*100/len(files))+'%'+'\n waiting time - ~'+str(round(execution_time*(len(files)-(files.index(file_path)))))+' sec...') print('commiting changes to the database\n') conn.commit() conn.close() if __name__ == '__main__': directory = '.' create_database() start = time.time() php_txt_files = get_php_txt_files(directory) end = time.time() - start print('Data is prepared:\n length of array = '+str(len(php_txt_files))+'\nTime taken - '+str(round(end,2))+ ' seconds') time.sleep(3) process_files(php_txt_files)