python - TypeError: %d format: a number is required, not getset_descriptor -
i trying code script finds out current date , time , creates folder name based on this. error when try run code:
typeerror: %d format: number required, not getset_descriptor
this code:
import os import time #import rpi.gpio gpio import logging import sys datetime import datetime d = datetime inityear = "%04d" % (d.year) initmonth = "%02d" % (d.month) initdate = "%02d" % (d.day) inithour = "%02d" % (d.hour) initmins = "%02d" % (d.minute)ion wish save files. set home default. # if run local web server on apache set /var/www/ make them # accessible via web browser. foldertosave = "/home/timelapse/timelapse_" + str(inityear) + str(initmonth) + str(initdate) + str(inithour) + str(initmins) #os.mkdir(foldertosave) # set initial serial saved images 1 fileserial = 1 = 'timefile' # run while loop of infinity while true: if os.path.isfile(a) == false: # set fileserialnumber 000x using 4 digits fileserialnumber = "%04d" % (fileserial) # capture current time (not start time set above) insert each capture image filename hour = "%02d" % (d.hour) mins = "%02d" % (d.minute) # define size of image wish capture. imgwidth = 800 # max = 2592 imgheight = 600 # max = 1944 print " ====================================== saving file @ " + hour + ":" + mins # capture image using raspistill. set capture added sharpening, auto white balance , average metering mode # change these settings see fit , suit conditions using camera in os.system("raspistill -w " + str(imgwidth) + " -h " + str(imgheight) + " -o " + str(foldertosave) + "/" + str(fileserialnumber) + "_" + str(hour) + str(mins) + ".jpg -sh 40 -awb auto -mm average -v") # increment fileserial fileserial += 1 # wait 10 minutes before next capture time.sleep(600) else: os.remove(time.txt) os.system("sudo shutdown -h -p now") break print "quitting now." sys.exit(0)
i think code has error here:
inityear = "%04d"
the error seems around "%04d" part. suggestions or appreciated. in advance.
you not creating datetime
instance here:
d = datetime
that new reference datetime
type. attributes d.year
, d.month
, etc. descriptors there, not values can interpolate:
>>> datetime import datetime >>> datetime.year <attribute 'year' of 'datetime.date' objects> >>> type(datetime.year) <type 'getset_descriptor'> >>> '%04d' % datetime.year traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: %d format: number required, not getset_descriptor
if wanted current timestamp, you'll need call datetime.now()
:
d = datetime.now()
there better ways use datetime value in string. use datetime.strftime()
method produce string format date:
formatted = d.strftime('%y%m%d%h%m') foldertosave = "/home/timelapse/timelapse_" + formatted
or can use same formatting codes in str.format()
interpolation:
foldertosave = "/home/timelapse/timelapse_{:%y%m%d%h%m}".format(d)
Comments
Post a Comment