如何将Python时间戳time.strftime插入到文件名输出的路径名中?

时间:2016-08-13 05:19:56

标签: python

目标是将日期时间戳作为Python脚本照片输出的文件名。

timestr=time.strftime("%Y%m%d-%H%M%S")

给出了我想要的print(timestr),但是我无法弄清楚如何将这个字符串插入到我给出的文件名中:

camera.capture('/path/to/save/file.jpg')

2 个答案:

答案 0 :(得分:0)

你已经完成了一半,现在只是尝试更改你要保存的文件名

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)          

答案 1 :(得分:0)

这是一个合适的解决方案:

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

输出变量将具有所需的结果