Last active
October 25, 2016 14:33
-
-
Save DJSagarAhire/32d07118f6158982abd4f380cc36e4e6 to your computer and use it in GitHub Desktop.
Quick script to create a list of directories named after dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_directories(basepath, start_date, end_date, date_format='%Y%m%d'): | |
""" Creates all subdirectories under the specified path named after the specified dates, one for each date. | |
start_date and end_date are both datetime.date objects. | |
date_format is a format string for the date which will be used to name the directories. | |
Example: | |
create_directories('/home/myhome', date(2016, 01, 01), date(2016, 01, 03)) will create directories named 20160101, 20160102 and 20160103 within /home/myhome. | |
Directory structure is created recursively if it doesn't exist. | |
""" | |
import datetime | |
import os | |
os.makedirs(basepath, exist_ok=True) | |
current_date = start_date | |
while current_date <= end_date: | |
current_date_string = '{0:{1}}'.format(current_date, date_format) | |
try: | |
os.mkdir('{0}/{1}'.format(basepath, current_date_string)) | |
except Exception as e: | |
print('Directory for date {0} already exists'.format(current_date_string)) | |
current_date += datetime.timedelta(days=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment