pythonbashmaxentfile-organization

How can I separate many files from one folder into separate folders based on the name of the files?


The current file organization looks like this:

Species_name1.asc
Species_name1.csv
Species_name1_Averages.csv
...
...
Species_name2.asc
Species_name2.csv
Species_name2_Averages.csv

I need to figure out a script that can create the new directories with the names (Species_name1, Species_name2... etc) and that can move the files from the base directory into the appropriate new directories.

import os
import glob
import shutil

base_directory = [CURRENT_WORKING_DIRECTORY]

with open("folder_names.txt", "r") as new_folders:
     for i in new_folders:
          os.mkdirs(base_directory+i)

Above is an example of what I can think of doing when creating new directories within the base directory.

I understand that I will have to utilize tools within the os, shutil, and/or glob modules if I were to use python. However, the exact script is escaping me and my files remain unorganized. If there is any advise you can provide in helping me complete this small task I will be most grateful.

Also there are many file types and suffixes within this directory but the (species_name?) portion is always consistent.

Below is the expected hierarchy:

Species_name1
-- Species_name1.asc
-- Species_name1.csv
-- Species_name1_Averages.csv
Species_name2
-- Species_name2.asc
-- Species_name2.csv
-- Species_name2_Averages.csv

Thank you in advance!


Solution

  • Assuming all your asc files are named like in your example:

    from os import  mkdir
    from shutil import move
    from glob import glob
    
    fs = []
    for file in glob("*.asc"):
        f = file.split('.')[0]
        fs.append(f)
        mkdir(f)
        
    for f in fs:
        for file in glob("*.*"):
            if file.startswith(f):
                move(file, f'.\\{f}\\{file}')
    


    UPDATE:

    Assuming all your Species_name.asc files are labeled like in your example:

    from os import  mkdir
    from shutil import move
    from glob import glob
    
    fs = [file.split('.')[0] for file in glob("Species_name*.asc")]
        
    for f in fs:
        mkdir(f)
        for file in glob("*.*"):
            if file.startswith(f):
                move(file, f'.\\{f}\\{file}')