title: How to move files stored in a directory and its subdirectories to a different directory?
body: <p>I have a ton of images stored in many subdirectories that need to be moved to a single directory. Many of these files have the exact same name, even though they may be present in different subdirectories.</p>
  <p>I have tried this, and it works perfectly. However, it is not able to access the files stores in subdirectories. What changes can I make so that it moves the files from subdirectories as well.</p>
  <pre><code>import os
  import shutil
  import datetime
  import glob
  
  now = str(datetime.datetime.now())
  now = now.replace(&quot;:&quot;,&quot;&quot;)
  count = 1
  
  dirr = 'path'
  move = '/path_to_directory_where_the_files_are_to_be_moved'
  for files in os.listdir(dirr): 
      if files.endswith('20.jpg') or files.endswith('29.jpg'):
          src = dirr + files
          dst = move + files+str(now)+'.jpg'
          shutil.copy(src, dst)
          print(&quot;copied &quot; + str(count) +' files \n')
          count +=1
  </code></pre>
  <p>I finally got it working, and I have shared the final code below.</p>
  <p>import os
  import shutil
  import datetime
  import glob
  import time
  count = 1</p>
  <pre><code>dirr = 'path'
  move = '/path_to_directory_where_the_files_are_to_be_moved'
  
  for files in glob.glob(dirr + '/**', recursive=True):
      if files.endswith('20.jpg') or files.endswith('29.jpg'):
          now = str(datetime.datetime.now())
          now = now.replace(&quot;:&quot;,&quot;&quot;)
          print(files)
          files1 = os.path.basename(files)
          src = dirr + files1
          dst = move + files1+str(now)+'.jpg'
          #print(src, dst)
          shutil.copy(files, dst)
          #time.sleep(0.05) ## use only when you want to make absolutely sure that each file gets a unique name when it is being copied. You won't need it anyway, but just in case
          print(&quot;copied &quot; + str(count) +' files \n')
          count +=1
  </code></pre>