Created
January 23, 2020 20:43
-
-
Save travis23/f41b131d877c0a939b1128c7efdb01c2 to your computer and use it in GitHub Desktop.
[Create Data Directory with Meaningful Name] #directory #python #pendulum
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
import os | |
import pendulum | |
def create_data_directory( | |
data_kind, # 'VolCal' | |
data_source_id, | |
timezone, | |
parent_directory | |
): | |
""" | |
Create a data directory with a meaningful name. Name contains a timestamp | |
that corresponds to when the directory was made. Directory creation might be immediately followed by | |
writing instrumentation data to the directory. | |
""" | |
start_datetime = pendulum.now(tz=timezone) | |
write_subdirectory = '{data_kind}_{data_source_id}_{year}-{month:02d}-{day:02d}' \ | |
'_{hour:02d}{minute:02d}{second:02d}{offset}'.format( | |
data_kind=data_kind, | |
data_source_id=data_source_id, | |
year=start_datetime.year, | |
month=start_datetime.month, | |
day=start_datetime.day, | |
hour=start_datetime.hour, | |
minute=start_datetime.minute, | |
second=start_datetime.second, | |
offset=start_datetime.offset_hours, | |
) | |
data_dir = os.path.join(parent_directory, write_subdirectory) | |
try: | |
os.mkdir(data_dir) | |
except FileExistsError: | |
pass | |
return data_dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment