[英]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.