简体   繁体   English

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

[英]How can I insert the Python timestamp time.strftime into a pathname for the filename output?

Goal is to put a date-timestamp as the filename for output of photos of a Python script. 目标是将日期时间戳记作为输出Python脚本照片的文件名。

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

gives me exactly what I want for print (timestr) but I cannot figure out how to insert this string for the file name into what I am given by: 给我确切我想要打印的内容(timestr),但是我不知道如何将文件名的此字符串插入我给的名称中:

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

you are halfway done, now just try to change the file name that you are saving 您已完成一半,现在只需尝试更改要保存的文件名

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)          

Here's a proper solution: 这是一个适当的解决方案:

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

output variable will have the desired result 输出变量将具有所需的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM