Incorrect Numbering of WebP Files in Directory Traverse Script

36 Views Asked by At

I wanted to create a code that would go through my directory and for folders in a specific position of the path, it would insert in a .txt, it creates in each different child folder, the number that those child folder's webp files are associated with in relation to the parent folder, alphabetically. Starting from 0 for each different parent folder (consider that there will be parent folders that are called in the same way, so to indentify each different parent folder I associated not just the name but the root before aswell, so they're truly unique).

However the code is NOT working as expected as it will number each like this:

  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\14 white" with contents: 1,2
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\15 grey" with contents: 3,4
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\22 red" with contents: 5,6
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\35 black" with contents: 7,8
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\9 blue" with contents: 9,10"

While it should've been:

  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\9 blue" with contents: 1,2"
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\14 white" with contents: 3,4
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\15 grey" with contents: 5,6
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\22 red" with contents: 7,8
  • Created "Image Position.txt" in "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\35 black" with contents: 9,10

Here is the code:

import os
import re

# Define the base directory
fold_A = r'C:\users\utente\desktop\randomizza colori'

# This dictionary will hold the count of webp files per design
design_webp_count = {}

# Traverse the directory structure
for root, dirs, files in os.walk(fold_A):
    # Check if there are any webp files in the current directory
    webp_files = [f for f in files if f.endswith('.webp')]
    if webp_files:
        # Extract the design name from the path (assuming it's the fourth folder in the path)
        path_parts = root.split(os.sep)
        if len(path_parts) > 4:
            design_name = path_parts[-2]
            root_before_design = os.sep.join(path_parts[:-2])  # Updated line to get all levels above {design}
            
            if (design_name, root_before_design) not in design_webp_count:
                design_webp_count[(design_name, root_before_design)] = 0
            # Count the webp files in this design folder
            design_webp_count[(design_name, root_before_design)] += len(webp_files)
            # Create the "Image Position.txt" file
            position_file_path = os.path.join(root, "Image Position.txt")
            with open(position_file_path, 'w') as position_file:
                # Write the comma-separated positions to the file
                positions = [str(design_webp_count[(design_name, root_before_design)] - len(webp_files) + i + 1) for i in range(len(webp_files))]
                position_file.write(','.join(positions))
                # Print the directory and the contents of the "Image Position.txt" file
                print(f'Created "Image Position.txt" in {root} with contents: {",".join(positions)}')

# Print "FINITO" once finished
print("FINITO")
0

There are 0 best solutions below