不正确地创建时间戳文件名(Improperly creating timestamped filenames)

我正在使用覆盆子pi和python脚本从连接到pi的相机捕获图像。 我有一个调用脚本的任务调度程序,脚本进行与摄像头连接的命令行调用。 我正在尝试让它创建一个新文件,标题为从python的datetime模块中获取的时间戳。

我遇到的问题是它不会同时打印。 我可以用日期时间戳创建一个文件; 我可以用时间创造一个。 但是,当我尝试将两者结合时,文件不会被创建,我不知道为什么。 是因为文件名最终太长了?

我已经尝试了以下代码的众多变体:

import os import time import datetime ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%y-%m-%d_%H:%M:%S") sys_call = 'fswebcam -r 1280x720 /home/pi/python/projectx_images/%s' %st os.system(sys_call)

我也尝试使用datetime.now()无济于事。 特别是,如果我尝试st = datetime.datetime.fromtimestamp(ts).strftime("%y-%m-%d')或st = datetime.datetime.fromtimestamp(ts).strftime("%H:%M:%S')那么它工作正常,但不是与两者兼而有之。

I'm using a raspberry pi and a python script to capture images from a camera connected to the pi. I have a task scheduler that calls the script, and the script make a command line call that interfaces with the camera. I'm trying to have it create a new file titled with the timestamp taken from python's datetime module.

The problem I'm experiencing is that it won't print both. I can create a file with just the date timestamp; I can create one with just the time. When I try to combine both, however, the file isn't created, and I'm not sure why. Is it because the filename ends up being too long?

I've tried numerous variations of the code below:

import os import time import datetime ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%y-%m-%d_%H:%M:%S") sys_call = 'fswebcam -r 1280x720 /home/pi/python/projectx_images/%s' %st os.system(sys_call)

I've also tried using datetime.now() to no avail. Specifically, if I try either st = datetime.datetime.fromtimestamp(ts).strftime("%y-%m-%d') or st = datetime.datetime.fromtimestamp(ts).strftime("%H:%M:%S') then it works fine, but not with both.

最满意答案

也许使用Python在这里是过度杀伤。 由于您的任务计划程序在调用Python时可能已经调用了一个shell,因此我们只需让shell执行此操作。

将此命令用于命令调度程序:

fswebcam -r 1280x720 /home/pi/python/projectx_images/$(date +%Y-%m-%d_%H:%M:%S)

Perhaps using Python is overkill here. Since your task scheduler is probably already invoking a shell when it invokes Python, let's just have the shell do the work.

Use this command for your command scheduler:

fswebcam -r 1280x720 /home/pi/python/projectx_images/$(date +%Y-%m-%d_%H:%M:%S)

更多推荐