Как сопоставлять и перемещать файлы в соответствующие папки с помощью Python

Я довольно новичок в программировании, и это мой первый опыт создания сложного скрипта с использованием Python.

Цель программы, которую я создаю:

просмотреть список файлов (всего 360 файлов в одной папке)извлечь 3 уникальных символа в имени файла и создать папку на основе 3 символов (всего 60 уникальных папок)создайте цикл for, который просматривает список файлов в исходной папке и перемещает его в соответствующую целевую папку.

Пример:

имя файла:KPHI_SDUS81_N3KDIX_201205261956

папка создана на основе символов:N3K

import os

#Creates a list based on file names in the folder
srcfile=os.listdir("E:\\Learning Python\\Testing out\\thunderstorm stuff")

#Directiory of where the source files are located
srcpath= "E:\\Learning Python\\Testing out\\thunderstorm stuff"

#Creates a list based on the location of where folders will be lcoated.
#List will be empty since for loop has not ran yet
targetsrc=os.listdir("E:\\Learning Python\\Testing out\\test folder")

#path of where the new folders created will be located
targetpath = "E:\\Learning Python\\Testing out\\test folder"

#empty list created to hold a string of 3 characters (see for loop below)
list=[]

#A list to hold the unique string values. (see 2nd for loop below)
Target=[]

#the for loop below looks at a file and gooes to the character place holder index of   12 and looks at the characters UP TO 15.
#It then assigns the three characters to a variable x which then passes the the string (the 3 characters) to an empty list called list. 
for num in srcfile:
  x=num[12:15]
  list.append(x)

#a test to see if the for loop above was able to exact the three characters from the list
print list
print srcfile

#created to see how big the list is which should match the amount of files in folder
print len(srcfile)
print len (list)

#a function created to make a folder based on a list
def create(s):
  targetpath = "E:\\Learning Python\\Testing out\\test folder"
  test=os.mkdir(os.path.join(targetpath,s))

#a dummy variable holder for the for loop below
valhold = "null"

#a nested if statement inside a for loop.
#The for loop goes through all the string values in a list called "list" (assigned to folder in for loop) 
#and checks it against a list called valhold.  If folder and valhold are not equal,
#the values in folder are appened to a list called Target.append which holds unique values.
#The next step is to create a folder a folder based off the list value "valhold"
for folder in list:
  if folder != valhold:
      Target.append(folder)
      valhold=folder
      create(valhold)
  else:
      valhold=folder

#a nested for loop which goes through all the files in the folder for the list "sourcefile"
#and finds a matching filename
for dst in Target:
  wheretonumber=0
  whereto = targetsrc(wheretonumber)  #Name of folder for a given index value "targetsrc"
  for file in list:
      filenumber=0
      filename=srcfile(filenumber) #Name of file for a given index value "sourcefile"
      if file == dst:
          ##os.rename(filename(filenumber),whereto(wheretonumber))
          ##shutil.move(filename,whereto)
      filenumber= filenumber+1
wheretonumber=wheretonumber+1

Я могу сделать первые две вещи из списка выше, но с трудом заставляю третью работать. Я'Мы просмотрели функции shutil.move, os.path.walk и os.rename и не смогли заставить их работать. Я продолжаю получать сообщение об ошибке:

whereto = targetrc (wheretonumber) TypeError: 'список' объект не вызывается

Я закомментировал os.rename и shutil.move, так как пробую разные функции. Правильна ли моя логика в подходе или я что-то упустил? Любые предложения по другим функциям, чтобы попробовать или изменить мой код, чтобы заставить его перемещать файлы в папку?

Ответы на вопрос(1)

Ваш ответ на вопрос