[英]How can I insert the Python timestamp time.strftime into a pathname for the filename output?
目标是将日期时间戳记作为输出Python脚本照片的文件名。
timestr=time.strftime("%Y%m%d-%H%M%S")
给我确切我想要打印的内容(timestr),但是我不知道如何将文件名的此字符串插入我给的名称中:
camera.capture('/path/to/save/file.jpg')
您已完成一半,现在只需尝试更改要保存的文件名
dir_path='path/to/save/'
timestr=time.strftime("%Y%m%d-%H%M%S")
file_name=timestr + '.jpg' #file name
path=dir_path+file_name #abs path of file
camera.capture(path)
这是一个适当的解决方案:
import time
import os
timestr, file_path = time.strftime("%Y%m%d-%H%M%S"), '/path/to/save/file.jpg'
filename, file_extension = os.path.splitext(file_path)
output = "{0}_{1}{2}".format(filename, timestr, file_extension)
print timestr, filename, output
输出变量将具有所需的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.