Reputation: 4405
I am trying to open a text file in python. In the following code:
import datime
from datetime import datetime
today = datetime.now()
sitename = "Soil Report"
rptFolder = r"C:\Template_Outputs\Production Reports"
file = open(rptFolder + "\\" + sitename + "_" + today.strftime("%y%b%d_%H:%M:%S") + ".txt", "w")
I keep getting the following error:
[Errno 22] invalid mode ('w') or filename:
I've narrowed it down the setting the date in the file name. It doesn't seem to like that (i.e. I just replaced the today.strftime("%y%b%d_%H:%M:%S") with "test" and it worked)
I've also tried to set the date as string to get it into the file name like:
str(today.strftime("%y%b%d_%H:%M:%S"))
No luck there either.
I need the time stamp in the file name because it the bigger script may be fun a few times in a short period.
is there something I'm missing here?
Upvotes: 0
Views: 1544
Reputation: 65599
If you're doing Windows, Windows doesn't allow files with colons (:). Try removing your colon from here:
str(today.strftime("%y%b%d_%H:%M:%S"))
and see what happens
Upvotes: 2
Reputation: 7619
the colons are generally not allowed in file names. replace with underscores. Also, you might want to include either milliseconds, nanoseconds, or a random number at the end in the event you run more than one file per second.
Upvotes: 1