-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathOptimized File Transfer Using Checksums.py
68 lines (53 loc) · 2.45 KB
/
Optimized File Transfer Using Checksums.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import hashlib
import os
import shutil
class OptimizedFileTransfer:
def __init__(self):
self.transferred_files = []
def hash_file(self, filepath):
"""Generate SHA-256 hash for a given file."""
try:
hasher = hashlib.sha256()
with open(filepath, 'rb') as f:
while chunk := f.read(8192):
hasher.update(chunk)
return hasher.hexdigest()
except FileNotFoundError:
print(f"File not found: {filepath}")
return None
def transfer_files(self, source_dir, dest_dir):
"""Transfer files from source to destination based on hash comparison."""
# Check if source directory exists
if not os.path.exists(source_dir):
print(f"Source directory not found: {source_dir}")
return
# Create destination directory if it doesn't exist
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
print(f"Destination directory created: {dest_dir}")
# Loop through files in source directory
for filename in os.listdir(source_dir):
src_file = os.path.join(source_dir, filename)
dest_file = os.path.join(dest_dir, filename)
print(f"Processing file: {src_file}") # Debug statement
src_hash = self.hash_file(src_file)
dest_hash = self.hash_file(dest_file) if os.path.exists(dest_file) else None
print(f"Source hash: {src_hash}, Destination hash: {dest_hash}") # Debug statement
if src_hash and src_hash != dest_hash:
shutil.copy2(src_file, dest_file)
self.transferred_files.append(filename)
print(f"Transferred file: {filename}")
else:
print(f"File unchanged: {filename}")
def get_transferred_files(self):
"""Return the list of transferred files."""
return self.transferred_files
# Test Cases
file_transfer = OptimizedFileTransfer()
# Use the absolute paths for your directories here
source_directory = "C:/Users/ASUS/Desktop/Rsp/AI-ML-DL-Hacktoberfest2024-WB/source_folder"
destination_directory = "C:/Users/ASUS/Desktop/Rsp/AI-ML-DL-Hacktoberfest2024-WB/destination_folder"
# Transfer files and handle directories
file_transfer.transfer_files(source_directory, destination_directory)
# List transferred files
print("Transferred Files:", file_transfer.get_transferred_files())