Created
April 20, 2022 14:26
-
-
Save amberj/007c5a52e75d9a1d8a5a10df06f4d85a to your computer and use it in GitHub Desktop.
Create folder (with date & time as folder name) in Python 3
This file contains 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
#!/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