![](/img/trans.png)
[英]Python copy files to a new directory and rename if file name already exists
[英]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.