2014-12-05 61 views
-2

我想編寫一個腳本,找出當前的日期和時間,然後創建一個基於此名稱的文件夾。 我得到這個錯誤,當我嘗試運行我的代碼:TypeError:%d格式:一個數字是必需的,而不是getset_descriptor

TypeError: %d format: a number is required, not getset_descriptor

這是我的代碼:

import os 
import time 
#import RPi.GPIO as GPIO 
import logging 
import sys 
from 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 where you wish to save files. Set to HOME as default. 
# If you run a local web server on Apache you could set this to /var/www/ to make them 
# accessible via web browser. 
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) +  str(initHour) + str(initMins) 
#os.mkdir(folderToSave) 
# Set the initial serial for saved images to 1 
fileSerial = 1 
a = 'timefile' 

# Run a WHILE Loop of infinity 
while True: 
    if os.path.isfile(a) == False: 
    # Set FileSerialNumber to 000X using four digits 
    fileSerialNumber = "%04d" % (fileSerial) 
    # Capture the CURRENT time (not start time as set above) to insert into each capture image filename 
    hour = "%02d" % (d.hour) 
    mins = "%02d" % (d.minute) 
    # Define the size of the image you wish to capture. 
    imgWidth = 800 # Max = 2592 
    imgHeight = 600 # Max = 1944 
    print " ====================================== Saving file at " + hour + ":" + mins 
    # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode 
    # Change these settings where you see fit and to suit the conditions you are using the 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 the 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) 

我覺得代碼在這裏有一個錯誤:

initYear = "%04d" 

錯誤似乎圍繞着「%04d」部分。任何建議或幫助,將不勝感激。提前致謝。

+0

你把*放入*格式字符串中是什麼?佔位符規格*不是問題*。 – 2014-12-05 20:26:25

+0

這不是你的錯誤所在。請提供回溯。 – Gerrat 2014-12-05 20:34:53

+0

@MartijnPieters我從「%04d」位的末尾刪除了「%(d.year)」,希望它可以工作,但它沒有幫助。 – 2014-12-05 20:36:37

回答

1

你是不是在這裏創造一個datetime實例:

d = datetime 

這只是一個新的參考datetime類型。屬性d.yeard.month等都是描述那裏,沒有價值,可以估算出:

>>> from 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: a number is required, not getset_descriptor 

如果你想當前的時間戳,您將需要調用datetime.now()

d = datetime.now() 

有是在字符串中使用日期時間值的更好方法。您可以使用datetime.strftime() method以生成一個字符串格式的日期:

formatted = d.strftime('%Y%m%d%H%M') 
folderToSave = "/home/timelapse/timelapse_" + formatted 

,或者你可以在str.format()插值使用相同的格式代碼:

folderToSave = "/home/timelapse/timelapse_{:%Y%m%d%H%M}".format(d) 
+0

工作正常!謝謝。 – 2014-12-05 20:44:12

0

您的問題是你的日期時間的使用。 datetime是一個類,而不是一個方法。

獲得當前的日期,使用方法:

d = datetime.now() 

它看起來像你試圖格式化各種日期組件。 但是,要引用它們,您需要使用這種格式:

inityear = str(d.year) 
相關問題