Move Files from One Directory to Another


Codes for Moving Files that Match a Certain Criterion from One Directory to Another

Published by Haerim Hwang

move files change directories criterion data science python

0 min READ

  • This script moves multiple files that match a certain criterion from one directory to another.


  • Codes

    • Step 1: Import modules

      import os
      import shutil
      import glob 
      


    • Step 2: Move files that match a certain criterion from one directory to another

      source = "source_directory"
              
      filenames = glob.glob("source_directory/*.txt")
              
      for filename in filenames:
          if "Korean.txt" in filename:
              destination = "source_directory/Korean/"
              shutil.move(filename, destination + filename) 
              
          elif "English.txt" in filename:
              destination = "source_directory/English/"
              shutil.move(filename, destination + filename)
              
          elif "Mandarin.txt" in filename:
              destination = "source_directory/Mandarin/"
              shutil.move(filename, destination + filename)