Ismail
Ismail

Reputation: 1

How to I reorganize a large number of files by filename?

I've got a small issue reorganizing files. I have about a million rainfall data files, with wierd names. I created this code to rename them into an understandable and manageable format. (eg: hmc.forcing-grid.195101010000.nc to 1951-01-01-0000.nc)

os.chdir(destination_directory)
for f in os.listdir():

    file_name, file_ext = os.path.splitext(f)
    f_1, f_2, date = file_name.split('.')
    digits = [int(d) for d in str(date)]
    year = (digits[0]*1000)+(digits[1]*100)+(digits[2]*10)+(digits[3])
    month = ((digits[4]*10)+digits[5])
    day = ((digits[6]*10)+digits[7])
    hour = ((digits[8]*10)+digits[9])
    newname = year,month,day,hour
    src = f
    dst = '{}{}{}'.format(destination_directory,newname, '.nc')
    os.rename(src,dst)
    print(destination_directory, newname, 'created')

Now all of my files get thrown into destination_directory with a new name and variables representing year,month, day and time. My problem now is that I need to reorganize them into different folders based on month. I essentially want my program to say:

create a new directory within destination_directory for this year/month and move all files with that year/month value into it

e.g: C:/data/1951-01-01-0000.nc --> C:/data/1951/01/1951-01-01-0000.nc

i'm just doing some geography work, so i've never had experience with programming, this is my first experience with a language, but thank you in advance !

(Note: don't worry about the indented os.chdir line, it's just so it appears as code on stackexchange )

P.S. : Fixed typo in question.

Upvotes: 0

Views: 83

Answers (1)

Lambo
Lambo

Reputation: 1174

I just changed your file name used in the example from "hmc.forcing-grid.1995101010000.nc" to "hmc.forcing-grid.195101010000.nc", else it did not work

wd = os.chdir(destination_directory)
for f in os.listdir():
    if os.path.isfile(f):
        file_name, file_ext = os.path.splitext(f)
        f_1, f_2, date = file_name.split('.')
        dt = datetime.strptime(date,"%Y%m%d%H%M")
        newname = "{}-{}-{}-{}.{}".format(str(dt.year),str(dt.month),str(dt.day),str(dt.time().hour)+str(dt.time().minute),'nc')
        dst = os.path.join(str(dt.year),str(dt.month),newname)
        os.makedirs(os.path.join(str(dt.year),str(dt.month)),exist_ok=True)
        os.rename(f, dst)
        print(dst, newname, 'created')

Upvotes: 1

Related Questions