簡體   English   中英

如何檢查目錄中已存在的同名文件並使用python重命名新文件保存?

[英]How do I check the existing file with same name in the directory and use python to rename new files to save?

我正在使用plt.savefig來保存由 matplotlib.pyplot 生成的 plot,每次運行代碼時我都想用不同的名稱保存我的圖形。 我怎樣才能做到這一點?

如果您不每秒保存多個文件,最簡單的方法可能是使用當前時間。

import datetime

ts = datetime.datetime.now().strftime("%Y%m%d-%H.%M.%S")
plt.savefig(f"figure-{ts}.png")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM