Create a tree directory (YYYY-MM-DD) using python

Published: 12 octobre 2016

DMCA.com Protection Status

Problem: I want to create a simple tree directory to store data with the following structure:

2008/
  ./2008/2008_01_01/
  ./2008/2008_01_02/
  ./2008/2008_01_03/
  ./2008/2008_01_04/
  ./2008/2008_01_05/
  ./2008/2008_01_06/
  ./2008/2008_01_07/
  ./2008/2008_01_08/
  ./2008/2008_01_09/
  ./2008/2008_01_10/
  ...
  ...
  ./2008/2008_12_28/
  ./2008/2008_12_29/
  ./2008/2008_12_30/
  ./2008/2008_12_31/

Soluce: create a python script

#!/usr/bin/env python

import numpy as np
import os
import calendar

year = 2008
#month = 1

command = 'mkdir ' + str(year)
os.system(command)

for month in range(1,13):
    for day in np.arange(calendar.monthrange(int(year),int(month))[1]):
        rep = './' + str(year) + '/' + \
              str(year) + '_' + "%02d" % month + '_' + "%02d" % (day+1) + '/'
        print rep
        command = 'mkdir ' + rep
        os.system(command)

References

Link WebSite
os python
calendar python