-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python - Parrallel Processing.txt
34 lines (29 loc) · 1.22 KB
/
Python - Parrallel Processing.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import sqlite3
import hashlib
import multiprocessing
def scan_directory(conn, dir_path):
# Use os.scandir to iterate over the files and directories in the tree
with os.scandir(dir_path) as entries:
for entry in entries:
# Check if the entry is a file or a directory
if entry.is_file():
# Calculate the file's hash and store it in the database
file_hash = hashlib.md5(entry.path.encode()).hexdigest()
c = conn.cursor()
c.execute("INSERT INTO files (path, hash) VALUES (?, ?)", (entry.path, file_hash))
conn.commit()
elif entry.is_dir():
# Recursively scan the subdirectory in a separate process
process = iprocessing.Process(target=scan_directory, args=(conn, entry.path))
process.start()
# Connect to the database
conn = sqlite3.connect("media_index.db")
# Create the files table if it doesn't exist
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS files (path text, hash text)")
conn.commit()
# Scan the root directory
scan_directory(conn, '/path/to/root/directory')
# Close the database connection
conn.close()