Skip to content

Instantly share code, notes, and snippets.

@amberj
Created April 20, 2022 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amberj/007c5a52e75d9a1d8a5a10df06f4d85a to your computer and use it in GitHub Desktop.
Save amberj/007c5a52e75d9a1d8a5a10df06f4d85a to your computer and use it in GitHub Desktop.
Create folder (with date & time as folder name) in Python 3
#!/usr/bin/env python3
from datetime import datetime
import os
now = datetime.now()
now_str = now.strftime("%m-%d-%Y_%H-%M-%S-%f")
# This will create a directory similar to this path:
# ./logs/04-20-2022_19-37-08/
temp_path="logs/" + now_str
# Using try/except as OSError may be raised due invalid path name etc
try:
# exist_ok=True suppresses the exception if folder already exists
os.makedirs(temp_path, exist_ok = True)
print("Created directory: ./" + temp_path + "/")
except OSError as error::
print("Error! Unable to create directory: ./" + temp_path + "/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment