I want to create a script that creates a file named after the current time and date in dd/mm/YY H:M:S format, like this:
from datetime import datetime
now = datetime.now()
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
open(str(dt_string), 'w+')
However, when I try to run it it returns FileNotFoundError: [Errno 2] No such file or directory: '25/06/2022 13:02:32'
Isn't Python supposed to create a new file, when you try to open one that doesn't exist?
I've tried using now as a string to use as a file name an it worked perfectly
from datetime import datetime
now = datetime.now()
# dd/mm/YY H:M:S
#dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
open(str(now), 'w+')
Why doesn't it accept dt_string as a file name?
I got it, thanks to FObersteiner
The problem was with '/' separators. I used dashes instead an everything worked perfectly. I also included ':' and there wasn't any problem with it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.