I have a script that runs hourly.
The output folder structure should be /todaysdate/hour/
So, at 00:00, the script will run & it should create both the todaysdate folder and a subfolder called 00.
At 01:00, the script will run - the todaysdate directory exists, so it should only create the subdirectory.
I've tried the below, but that doesn't work - how would I approach this?
file_path = 'Desktop/%s/%s' %(today_date, hour)
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
You stated that you want to create directories for each hour. So you don't need to get the dirname. This should do it:
file_path = 'Desktop/%s/%s' %(today_date, hour)
if not os.path.exists(file_path):
os.makedirs(file_path)
See docs for os.path.dirname
. This gives you the directory that contains file_path
, eg
file_path = "Desktop/22-10-2018/00"
print(os.path.dirname(file_path))
>>> "Desktop/22-10-2018/"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.