Advertisement
nicuf

Compare folder files vs subfolder files - Cristian

Jan 7th, 2024
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import os
  2.  
  3. # Set the paths to the folders you want to compare
  4. folder_a = r"c:\Folder-Oana\extracted"
  5. folder_b = r"c:\Folder-Oana\extracted\translated"
  6.  
  7. # Define a function to get a list of all files, including subfolders
  8. def get_all_files(folder):
  9.     # Initialize an empty list to store the file paths
  10.     all_files = []
  11.  
  12.     # Use the os.walk function to walk through the directory tree
  13.     for root, dirs, files in os.walk(folder):
  14.         # Loop through the files in the current directory
  15.         for file in files:
  16.             # Append the file path to the all_files list
  17.             all_files.append(os.path.join(root, file))
  18.         # Loop through all the subdirectories in the current directory - NEW
  19.         for dir in dirs:
  20.             # Get all files in each subdirectory
  21.             subdir_files = get_all_files(os.path.join(root, dir))
  22.             all_files = all_files + subdir_files
  23.     # Return the list of file paths
  24.     return all_files
  25.  
  26. if __name__ == '__main__':
  27.     # Get the lists of all files in folder_a and folder_b
  28.     files_in_folder_a = get_all_files(folder_a)
  29.     files_in_folder_b = get_all_files(folder_b)
  30.    
  31.     # Convert the lists to sets for faster comparison
  32.     set_a = set(files_in_folder_a)
  33.     set_b = set(files_in_folder_b)
  34.    
  35.     # Find the files that are in folder_a but not in folder_b
  36.     not_in_b = set_a - set_b
  37.     # Print the filenames that are not in folder_b
  38.     print("The following files are in folder A but not in folder B:\n")
  39.     for file in not_in_b:
  40.         print(file)
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement