title: Move pairs of files (.txt & .xml) into their corresponding folder using Python
body: <p>I have been working this challenge for about a day or so. I've looked at multiple questions and answers asked on SO and tried to 'MacGyver' the code used for my purpose, but still having issues.</p>
  
  <p>I have a directory (lets call it "src\") with hundreds of files (.txt and .xml). Each .txt file has an associated .xml file (let's call it a pair). Example:</p>
  
  <pre><code>src\text-001.txt
  src\text-001.xml
  src\text-002.txt
  src\text-002.xml
  src\text-003.txt
  src\text-003.xml
  </code></pre>
  
  <p>Here's an example of how I would like it to turn out so each pair of files are placed into a single unique folder:</p>
  
  <pre><code>src\text-001\text-001.txt
  src\text-001\text-001.xml
  src\text-002\text-002.txt
  src\text-002\text-002.xml
  src\text-003\text-003.txt
  src\text-003\text-003.xml
  </code></pre>
  
  <p>What I'd like to do is create an associated folder for each pair and then move each pair of files into its respective folder using Python. I've already tried working from code I found (thanks to a post from Nov '12 by <a href="https://stackoverflow.com/users/896021/sethdd">Sethdd</a>, but am having trouble figuring out how to use the move function to grab pairs of files. Here's where I'm at:</p>
  
  <pre><code>import os
  import shutil
  
  srcpath = "PATH_TO_SOURCE"
  srcfiles = os.listdir(srcpath)
  
  destpath = "PATH_TO_DEST"
  
  # grabs the name of the file before extension and uses as the dest folder name
  destdirs = list(set([filename[0:9] 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[0:9]:
              move(filename, full_path)
  </code></pre>
  
  <p>I feel like it should be easy, but Python isn't something I work with everyday and it's been a while since my scripting days... Any help would be greatly appreciated!</p>
  
  <p>Thanks,</p>
  
  <p>WK2EcoD</p>