title: I want to move a file based on part of the name to a folder with that name
body: <p>I have a directory with a large number of files that I want to move into folders based on part of the file name. My list of files looks like this:</p>
  <pre><code>001-020-012B-B.nc
  001-022-151-A.nc
  001-023-022-PY-T1.nc.nc
  001-096-016B-A.nc
  </code></pre>
  <p>I want to move the files I have into separate folders based on the first part of the file name (<code>001-096-016B</code>, <code>001-023-022</code>, <code>001-022-151</code>). The first parts of the file name always have the same number of numbers and are always in 3 parts separated by an underscore <code>'-'</code>.</p>
  <p>The folder names are named like this <code>\oe-xxxx\xxxx\xxxx\001-Disc-PED\020-Rotor-parts-1200</code>.</p>
  <p>So for example, this file should be placed in the above folder, based on the folder name (the numbers):</p>
  <pre><code>001-020-012B-B.nc
  </code></pre>
  <p>File path divided into column to show where the above file has to be moved to:<br />
  <code>(001)-Disc-PED\(020)-Rotor-parts-1200</code>.</p>
  <p>Therefore:</p>
  <pre><code>(001)-Disc-PED\(020)-Rotor-parts-1200 (001)-(020)-012B-B.nc
  </code></pre>
  <p>This is what I have tried from looking online but it does not work:
  My thinking is I want to loop through the folders and look for matches.</p>
  <pre><code>import os
  import glob
  import itertools
  import re
  
  #Source file
  sourcefile = r'C:\Users\cah\Desktop\000Turning'
  destinationPath = r'C:\Users\cah\Desktop\08-CAM'
  
  #Seperation
  dirs = glob.glob('*-*')
  
  #Every file with file extension .nc
  files = glob.glob('*.nc')
  
  for root, dirs, files in os.walk(sourcefile):
      for file in files:
          if file.endswith(&quot;.nc&quot;):
  
              first3Char = str(file[0:3])
              last3Char = str(file[4:7])
  
              for root in os.walk(destinationPath):
  
                  first33CharsOfRoot = str(root[0:33])
  
                  cleanRoot1 = str(root).replace(&quot;[&quot;, &quot;&quot;)
                  cleanRoot2 = str(cleanRoot1).replace(&quot;]&quot;, &quot;&quot;)
                  cleanRoot3 = str(cleanRoot2).replace(&quot;)&quot;, &quot;&quot;)
                  cleanRoot4 = str(cleanRoot3).replace(&quot;'&quot;, &quot;&quot;)
                  cleanRoot5 = str(cleanRoot4).replace(&quot;,&quot;, &quot;&quot;)
  
                  firstCharOfRoot = re.findall(r'(.{3})\s*$', str(cleanRoot5))
  
                  print(firstCharOfRoot==first3Char)
  
                  if(firstCharOfRoot == first3Char):
                      print(&quot;Hello&quot;)
                      for root in os.walk(destinationPath):
                          print(os.path.basename(root))
  
                       #   if(os.path)
  </code></pre>
  <p>I realized that I should not look for the last 3 chars in the path, because it is the first (001) etc. Numbers that I need to look for in the beginning to find the first path that I need to go to.</p>
  <p>EDIT:</p>
  <pre><code>    import os
  import glob
  import itertools
  import re
  
  #Source file 
  sourcefile = r'C:\Users\cah\Desktop\000Turning'
  destinationPath = r'C:\Users\cah\Desktop\08-CAM'
  
  #Seperation
  dirs = glob.glob('*-*')
  
  #Every file with file extension .nc
  files = glob.glob('*.nc')
  
  
  
  for root, dirs, files in os.walk(sourcefile):
      for file in files:
          if file.endswith(&quot;.nc&quot;):
              
              first3Char = str(file[0:3])
              last3Char = str(file[4:7])
  
  
              for root in os.walk(destinationPath):
                  
                  
              
  
                  cleanRoot1 = str(root).replace(&quot;[&quot;, &quot;&quot;)
                  cleanRoot2 = str(cleanRoot1).replace(&quot;]&quot;, &quot;&quot;)
                  cleanRoot3 = str(cleanRoot2).replace(&quot;)&quot;, &quot;&quot;)
                  cleanRoot4 = str(cleanRoot3).replace(&quot;'&quot;, &quot;&quot;)
                  cleanRoot5 = str(cleanRoot4).replace(&quot;,&quot;, &quot;&quot;)
  
                  firstCharOfRoot = re.findall(r'^(?:[^\\]+\\\\){5}(\d+).*$', str(cleanRoot5))
                  secondCharOfRoot = re.findall(r'^(?:[^\\]+\\\\){6}(\d+).*$', str(cleanRoot5))
                  
  
                  firstCharOfRootCleaned = ''.join(firstCharOfRoot)
                  secondCharOfRoot = ''.join(secondCharOfRoot)
  
  
                  cleanRoot6 = str(cleanRoot5).replace(&quot;(&quot;, &quot;&quot;)
  
                  if(firstCharOfRootCleaned == str(first3Char) &amp; secondCharOfRoot == str(last3Char)):
  
                      print(&quot;BINGOf&quot;)
                    #  for root1 in os.walk(cleanRoot6):
                     
  </code></pre>