29 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			29 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
import os
 | 
						|
import shutil
 | 
						|
 | 
						|
def organize_files_by_username(folder_path, dest_folder_path):
 | 
						|
    # Lấy danh sách các tệp trong thư mục
 | 
						|
    files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
 | 
						|
    
 | 
						|
    for file in files:
 | 
						|
        # Kiểm tra định dạng tên tệp: <username>_checkin_date.png
 | 
						|
        if "_" in file and file.endswith(".png"):
 | 
						|
            username = file.split("_")[0]  # Lấy phần username từ tên tệp
 | 
						|
            
 | 
						|
            # Tạo đường dẫn thư mục con
 | 
						|
            subfolder_path = os.path.join(folder_path, username)
 | 
						|
            
 | 
						|
            # Tạo thư mục con nếu chưa tồn tại
 | 
						|
            if not os.path.exists(subfolder_path):
 | 
						|
                os.makedirs(subfolder_path)
 | 
						|
            
 | 
						|
            # Di chuyển tệp vào thư mục con
 | 
						|
            shutil.move(os.path.join(folder_path, file), os.path.join(subfolder_path, file))
 | 
						|
    
 | 
						|
    print("Hoàn thành sắp xếp tệp theo username.")
 | 
						|
 | 
						|
# Đường dẫn tới thư mục chứa các tệp
 | 
						|
folder_path = "/home/joseph/screenshot"
 | 
						|
dest_folder_path = "/home/joseph/DetectFace/dataset"
 | 
						|
organize_files_by_username(folder_path, dest_folder_path)
 |