Skip to content
Advertisement

move files to subdirectories that are named on part of the filenames

I have a few data files in a directory, and I want to move them to the subdirectories based on their filenames. Let’s say we created the first directory named “20220322_170444,” and it should contain the first four files only because in the next file the “el” is less than the previous one, so the second folder, let’s say is “20220322_170533”, then it should contain next eight files until the el becomes less again than the previous name.

example data

files =[
'cfrad.20220322_170444.122_COW1_v2_s02_el3.40_SUR.nc',
'cfrad.20220322_170456.550_COW1_v2_s03_el4.22_SUR.nc',
'cfrad.20220322_170508.975_COW1_v2_s04_el5.09_SUR.nc',
'cfrad.20220322_170521.397_COW1_v2_s05_el5.99_SUR.nc',
'cfrad.20220322_170533.811_COW1_v2_s06_el0.45_SUR.nc',
'cfrad.20220322_170546.228_COW1_v2_s07_el1.20_SUR.nc',
'cfrad.20220322_170558.648_COW1_v2_s08_el1.90_SUR.nc',
'cfrad.20220322_170611.072_COW1_v2_s09_el2.61_SUR.nc',
'cfrad.20220322_170623.503_COW1_v2_s10_el3.40_SUR.nc',
'cfrad.20220322_170635.923_COW1_v2_s11_el4.21_SUR.nc',
'cfrad.20220322_170648.341_COW1_v2_s12_el5.09_SUR.nc',
'cfrad.20220322_170700.765_COW1_v2_s13_el5.99_SUR.nc',
'cfrad.20220322_170713.179_COW1_v2_s14_el0.45_SUR.nc',
'cfrad.20220322_170725.604_COW1_v2_s15_el1.20_SUR.nc',
'cfrad.20220322_170738.030_COW1_v2_s16_el1.90_SUR.nc',
'cfrad.20220322_170750.461_COW1_v2_s17_el2.61_SUR.nc',
'cfrad.20220322_170802.877_COW1_v2_s18_el3.40_SUR.nc',
'cfrad.20220322_170815.301_COW1_v2_s19_el4.22_SUR.nc',
'cfrad.20220322_170827.715_COW1_v2_s20_el8.01_SUR.nc',
'cfrad.20220322_170840.144_COW1_v2_s21_el11.02_SUR.nc']
for file in files:
    np.savetxt(fname=file, X=np.array([1,1]))

What I tried is

import numpy as np
from datetime import datetime
import glob, os, re
import shutil

sweeps = []
temp = []
for i, file in enumerate(files[:19]):
    match_str = re.search(r'd{4}d{2}d{2}_d{2}d{2}d{2}', file)
    res = datetime.strptime(match_str.group(), '%Y%m%d_%H%M%S')
    print(res.strftime("%Y%m%d_%H%M%S"))
    el_pos = int(files[i].find('el'))
    st_pos = files[i][el_pos+1:el_pos+3]
    el_pos1 = int(files[i+1].find('el'))
    end_pos = files[i+1][el_pos1+1:el_pos1+3]
#     print(files[i][s_pos+1:s_pos+3],files[i+1][s_pos1+1:s_pos1+3])
    temp.append(files[i])
    print("len(files):",len(files),i)
    print(st_pos,end_pos)
#     print()
    if st_pos>end_pos:
        print("temp len: ", len(temp))
        sweeps.append(temp)
        temp = []
    elif len(files)-i==2:
        print('entered')
        sweeps.append(temp)

I now have a list named sweeps, and it contains the desired files; how can I now move these files to the directories,m but the directories should be named as I stated above based on the date. I have also the date string in variable res.strftime(“%Y%m%d_%H%M%S”) can be used to create directories.

Advertisement

Answer

Some string splitting can do this for you.

import shutil
import os


files = [
    "cfrad.20220322_170444.122_COW1_v2_s02_el3.40_SUR.nc",
    "cfrad.20220322_170456.550_COW1_v2_s03_el4.22_SUR.nc",
    "cfrad.20220322_170508.975_COW1_v2_s04_el5.09_SUR.nc",
    "cfrad.20220322_170521.397_COW1_v2_s05_el5.99_SUR.nc",
    "cfrad.20220322_170533.811_COW1_v2_s06_el0.45_SUR.nc",
    "cfrad.20220322_170546.228_COW1_v2_s07_el1.20_SUR.nc",
    "cfrad.20220322_170558.648_COW1_v2_s08_el1.90_SUR.nc",
    "cfrad.20220322_170611.072_COW1_v2_s09_el2.61_SUR.nc",
    "cfrad.20220322_170623.503_COW1_v2_s10_el3.40_SUR.nc",
    "cfrad.20220322_170635.923_COW1_v2_s11_el4.21_SUR.nc",
    "cfrad.20220322_170648.341_COW1_v2_s12_el5.09_SUR.nc",
    "cfrad.20220322_170700.765_COW1_v2_s13_el5.99_SUR.nc",
    "cfrad.20220322_170713.179_COW1_v2_s14_el0.45_SUR.nc",
    "cfrad.20220322_170725.604_COW1_v2_s15_el1.20_SUR.nc",
    "cfrad.20220322_170738.030_COW1_v2_s16_el1.90_SUR.nc",
    "cfrad.20220322_170750.461_COW1_v2_s17_el2.61_SUR.nc",
    "cfrad.20220322_170802.877_COW1_v2_s18_el3.40_SUR.nc",
    "cfrad.20220322_170815.301_COW1_v2_s19_el4.22_SUR.nc",
    "cfrad.20220322_170827.715_COW1_v2_s20_el8.01_SUR.nc",
    "cfrad.20220322_170840.144_COW1_v2_s21_el11.02_SUR.nc",
]


for f in files:
    with open(f, "w") as of:
        of.write("n")

# force the if statement below to be True on first run
el = 99999999
basepath = "."


for f in files:
    new_el = int(f.split(".")[2].split("_")[-1].replace("el", ""))
    if new_el < el:
        # store new dir name
        curr_dir = f.split(".")[1]
        print(curr_dir)
        # create directory
        os.makedirs(curr_dir, exist_ok=True)
    # store new el
    el = new_el
    # move file
    shutil.move(f"{basepath}{os.sep}{f}", f"{basepath}{os.sep}{curr_dir}{os.sep}{f}")

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement