# Actually 2 different scripts. One copies files from many folders into one main folder
# or the other: one folder into many subfolder
# These scripts are very specific to monthly data, as this is sometimes needed to be in folders
# by month and sometimes in one folder alltogether
import os, shutil
###############################################################################
#COPY MANY TO ONE
###############################################################################
# in this folder are many folders, all files will be copied into the outer Folder
# eg monthly files of several years, where a separate folder for January, February etc. exists
# Output folder is the one folder containing the subfolders
inFol = '.../main_folder/'
for fols in os.listdir(inFol):
if os.path.isdir(inFol+fols):
for files in os.listdir(inFol+fols):
shutil.copyfile(inFol+fols+"/"+files,inFol+"/"+files)
###############################################################################
#COPY ONE TO MANY
###############################################################################
# copy monthly files that are in one folder (e.g. OutMonth) into monthly folders
# (01_Jan 02_Feb etc.) within this first folder. Input files should be in the form
# 1999_01.tif
# You may need to change startYear and endYear
startYear = 1999
endYear = 2013
inFol = '.../main_folder/'
# Lists with month numbers and names
monthNum = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
monthName = ["Jan", "Feb","Mar","Apr","May","Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"]
for moNum,moName in zip(monthNum,monthName): # parallel iteration over month numbers and names
curDir = inFol+moNum + "_" + moName #current directory
#if curDir already exists, delete it
if os.path.isdir(curDir):
os.remove(curDir)
os.mkdir(curDir)
for yeari in range(startYear,endYear+1):
year = str(yeari)
shutil.copyfile(inFol + year + "_" + moNum + ".tif",
curDir + "/" + year + "_" + moNum + ".tif")