Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:16:17+00:00 2026-06-14T12:16:17+00:00

I am fairly new to programing and this is my first stab at creating

  • 0

I am fairly new to programing and this is my first stab at creating a complex script using Python

The purpose of the program I am creating is:

  • to go through a list of files (360 files altogether in a single folder)
  • extract 3 unique characters in the file name and create a folder based on the 3 characters (60 unique folders altogether)
  • create a for loop which goes through the list of files in the source folder and moves it to its corresponding target folder.

Example:

file name: KPHI_SDUS81_N3KDIX_201205261956

folder created based on characters: 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

I am able to do the first 2 things in my bullet point list above but having a hard time getting the 3rd one to work. I’ve looked into shutil.move, os.path.walk, and os.rename functions and have had no luck getting them to work. I keep getting the error:

whereto = targetsrc(wheretonumber)
TypeError: ‘list’ object is not callable

I have the os.rename and shutil.move commented out since I am trying different functions. Is my logic correct in the approach or am I missing something? Any suggestions on other functions to try or changes to my code to get it to move the files into a folder?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:16:19+00:00Added an answer on June 14, 2026 at 12:16 pm

    To remove duplicates from the list variable just use set() built-in. And don’t use list as a variable name, this shadows built-in list().

    Lists are indexed with brackets [] not parens.

    I can’t see where you’re assigning anything but empty list (you wrote it yourself: #List will be empty since for loop has not ran yet) to targetsrc. Empty list has no elements, so even L[0] will be out of range.

    Try something like this:

    import os
    import shutil
    
    srcpath = "E:\\Learning Python\\Testing out\\thunderstorm stuff"
    srcfiles = os.listdir(srcpath)
    
    destpath = "E:\\Learning Python\\Testing out\\test folder"
    
    # extract the three letters from filenames and filter out duplicates
    destdirs = list(set([filename[12:15] for filename in srcfiles]))
    
    
    def create(dirname, destpath):
        full_path = os.path.join(destpath, dirname)
        os.mkdir(full_path)
        return full_path
    
    def move(filename, dirpath):
        shutil.move(os.path.join(srcpath, filename)
                    ,dirpath)
    
    # create destination directories and store their names along with full paths
    targets = [
        (folder, create(folder, destpath)) for folder in destdirs
    ]
    
    for dirname, full_path in targets:
        for filename in srcfile:
            if dirname == filename[12:15]:
                move(filename, full_path)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to C# (this is my first C# project). I'm fairly confident
I'm fairly new to C# and WPF programing and right now this problem is
I'm fairly new to Android programming, so this may be a simple question, but
I am fairly new to programming and to python and wxpython. I have looked
I am fairly new to Python programming and Threads isn't my area of expertise.
I'm fairly new to database programming in WinForms, and have been using BindingSource, DataSet,
First off, i'm fairly new to programming, I've built a few asmx web services
Ok I will start by saying I'm fairly new to programing android applications. That
Is there any reason I shouldn't do this? I'm fairly new at programming iPhone
I'm fairly new to OpenCL so please bear with me. In the first iteration

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.