Я знаю, почему получаю следующую ошибку, но у меня вопрос: есть ли более умный способ сделать это? Я также хотел бы переименовать все повторяющиеся имена каталогов, но так и не понял...
Вот мой код:
Код: Выделить всё
#!/usr/bin/env python3.6
import os
import glob
path = os.getcwd()
file_list = []
duplicates = {}
# remove filename duplicates
for file_path in glob.glob(path + "/**/*.c", recursive=True):
file = file_path.rsplit('/', 1)[1]
if file not in file_list:
file_list.append(file)
else:
if file in duplicates.keys():
duplicates[file] += 1
lista = []
lista.append(file_path)
os.rename(
file_path,
file_path.rsplit('/', 1)[:-1]
+ '/'
+ str(duplicates[file])
+ file)
else:
duplicates[file] = 1
os.rename(
file_path,
file_path.rsplit('/', 1)[:-1]
+ '/'
+ str(duplicates[file])
+ file)
Код: Выделить всё
Traceback (most recent call last):
File "/home/andre/Development/scripts/removeDuplicates.py", line 22, in
os.rename(file_path, file_path.rsplit('/', 1)[:-1] + '/' + str(duplicates[file]) + file)
TypeError: can only concatenate list (not "str") to list
Подробнее здесь: https://stackoverflow.com/questions/440 ... ing-python
Мобильная версия