获取日期并将其保存为文件名python

shl*_*omi 5 python date save

我有一些图表,我在python中做.

最后我将绘图保存为png文件.这是代码:

plt.scatter(LuxCoordinates, finalPix, linewidths=1)
plt.sca
plt.grid(axis)
plt.xlabel('Ambient', color='r');
plt.ylabel('Depth Grows', color='r'); # grayscale color
plt.title(PngName, color='b');
savefig(PngName+'.png'); #PngName is the name of the file that the user gives in argv
Run Code Online (Sandbox Code Playgroud)

这项工作很好,它保存了一个文件,名称PngName.png(其中PngName是用户决定的)

现在我想将该名称添加到当前日期.我试过这样做:

date = time.strftime("%d/%m/%Y")
plt.scatter(LuxCoordinates, finalPix, linewidths=1)
plt.sca
plt.grid(axis)
plt.xlabel('Ambient', color='r');
plt.ylabel('Depth Grows', color='r'); # grayscale color
plt.title(PngName, color='b');
savefig(PngName+'_'+date+'.png')
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我收到此错误消息:

filename_or_obj = open(filename_or_obj,'wb')IOError:[Errno 2]没有这样的文件或目录:'05/12/2013.png'

您可以看到日期变量获取日期.(当我将它打印到屏幕上时,我看到了日期)

有什么问题,如何解决?

谢谢!

Dar*_*one 6

您的文件名05/12/2013.png包含斜杠 ( /),这些很可能是您操作系统上的路径名分隔符。换句话说,您尝试写入的文件名2013.png位于 目录12下的 目录 中05。如果这不是您想要的,那么考虑将您的文件名格式更改为类似time.strftime("%Y-%m-%d"), 或任何其他不带斜杠的格式。