shlomi
shlomi

Reputation: 549

get date and save it as a file name python

I have some graph that I do in python.

In the end I save the plot to a png file. Here is the code:

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

This work good and it save me a file, name PngName.png (Where PngName is what the user decided)

Now I want to add to that name the current date. I had try to do this:

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')

But it's not working, I get this error message:

filename_or_obj = open(filename_or_obj, 'wb') IOError: [Errno 2] No such file or directory: '05/12/2013.png'

You can see that the date variable get the date. (also when I'm printing it to screen I see the date)

What is the problem and how can I solve it?

Thanks!

Upvotes: 6

Views: 3309

Answers (2)

user9329768
user9329768

Reputation:

Slightly adding to the answer given by Darren Stone:

# Time stamp with date and time
time.strftime("%Y-%m-%d %H%M%S")

# save plot
savefig("Figure "+ time.strftime("%Y-%m-%d %H%M%S") + ".png")

Upvotes: 1

Darren Stone
Darren Stone

Reputation: 2068

Your filename 05/12/2013.png contains slashes (/) and these are likely pathname separator characters on your OS. In other words, the filename you are trying to write to is 2013.png in the directory 12, under the directory 05. If that's not what you want, then consider changing your filename format to something like time.strftime("%Y-%m-%d"), or anything else without slashes.

Upvotes: 7

Related Questions