nikita1221
nikita1221

Reputation: 87

python: datetime strftime not working properly with file naming

here is my code:

from datetime import date
from datetime import datetime

time = date.today().strftime("%Y-%m-%d-%H-%M-%S")
f = open(r'C:/folder' + time + '.txt', 'w')
f.write("Something")
f.close()

But output is e.g. 2022-06-13-00-00-00 at 14:19:53. Could you, please, hint me how to fix it?

Upvotes: 0

Views: 51

Answers (2)

Lucifer
Lucifer

Reputation: 164

Try using this -

 from datetime import datetime
 time_curr = datetime.now()
 print(time_curr)

Upvotes: 0

Vvamp
Vvamp

Reputation: 414

You are using date.today(), which returns today's date without any time set.
You want to use datetime.now(), which returns the date along with the time.

So, you should change your code to the following.

from datetime import datetime

time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
f = open(r'C:/folder' + time + '.txt', 'w')
f.write("Something")
f.close()

Upvotes: 3

Related Questions