Однако код НЕ работает должным образом, поскольку каждый из них будет нумероваться следующим образом:
- Создано «Положение изображения». .txt" в "C:\users\utente\desktop\randomizza colori\Boxy Hoodie\Both\Back\0009 INSECURITY\14 white" с содержимым: 1,2
- Создано "Изображение" Position.txt" в "C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\15 grey" с содержимым:
3,4 - Создан «Image Position.txt» в «C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\22 red» с содержимым:
5,6 - Создан «Image Position.txt» в «C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\ 35 black» с содержимым:
7,8 - Создан «Image Position.txt» в «C:\users\utente\desktop\randomizza
colori\Boxy» Толстовка\Оба\Спина\0009 INSECURITY\9 синяя" с содержимым:
9,10"
- Создан «Image Position.txt» в «C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009» INSECURITY\9 blue" с содержимым:
1,2" - Создан "Image Position.txt" в "C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\14 white" с содержимым:
3,4 - Создан "Image Position.txt" в "C:\users\" utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\15 grey" с содержимым:
5,6 - Создал "Положение изображения. txt" в "C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\22 red" с содержимым:
7,8 - Создан «Image Position.txt» в «C:\users\utente\desktop\randomizza
colori\Boxy Hoodie\Both\Back\0009 INSECURITY\35 black» с содержимым:
9 ,10
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")
Подробнее здесь: https://stackoverflow.com/questions/779 ... rse-script