python程序的开始时间

时间:2016-11-09 12:47:58

标签: python raspberry-pi

我有一个覆盆子pi 3(os:rasbian),我想在13:00运行此代码为exp 当我离开家时12:00但我希望程序将在1小时后开始。 我该如何修改程序。 感谢

import time
import picamera
with picamera.PiCamera() as camera:
    camera.start_preview()
    try:
        for i, filename in enumerate(camera.capture_continuous('/home/pi/Google Drive/{timestamp:%H-%M-%S}-{counter:03d}.jpg')):
            print(filename)
            time.sleep(3)
            if i == 3:
                break
    finally:
        camera.stop_preview()

1 个答案:

答案 0 :(得分:1)

我会这样做:

import time
import picamera

# Pause program until Enter is pressed (press when You leave the home)
start = raw_input('Press Enter to start the counter: ') # or input(), if You use Python3

# Show message
print('Camera will start recording in 1 hour')

# Capture Ctrl-C
try:

    # Sleep for an hour
    time.sleep(3600)

# Maybe You want to start immediately, pressed Ctrl-C
except KeyboardInterrupt:
    print('Starting camera now')

with picamera.PiCamera() as camera:
    camera.start_preview()
    try:
        for i, filename in enumerate(camera.capture_continuous('/home/pi/Google Drive/{timestamp:%H-%M-%S}-{counter:03d}.jpg')):
            print(filename)
            time.sleep(3)
            if i == 3:
                break
    finally:
        camera.stop_preview()

这样,您可以随时启动程序并让它在后台等待。你离开前激活它。如果需要,您也可以强制它立即启动。