0

I get this error when I try to use the open function. If I understand correctly, if the file doesnt exist (it doesn't), it is automatically created. I tried changing the 'w' to 'w+', but I got the same error.

Traceback (most recent call last):
  File "altdl.py", line 24, in <module>
    sys.stdout = open(str(start_dir + "\\Logs\\" + "log_" + str(now.date()) + "_
" + str(now.time()) + ".log"), 'w')
IOError: [Errno 22] invalid mode ('w') or filename: 'C:\\Users\\Vaibhav\\Desktop
\\Test\\Logs\\log_2013-07-02_11:21:37.717000.log'

By the way, start_dir is set as str(os.getcwd()).

fvrghl
  • 3,642
  • 5
  • 28
  • 36
Vaibhav Aggarwal
  • 1,381
  • 2
  • 19
  • 30

2 Answers2

0

Unfortunately, Windows hates colons in path names.

Try something like:

 with open("C:\\Users\\Vaibhav\\Desktop\\Test\\Logs\\log_2013-07-02_11.21.37.717000.log", "w") as f:
    pass
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
0

As said in the other answer, Windows doesn't really like colons. So, let's try to format your time / date using another character:

import datetime
filename = "log_{}.log".format(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
# filename should be something like : 'log_2013-07-02_20-47-06.log'
f = open(filename, 'w')

Et voilà !

Also, I'd recommend using os.joinpath instead of hardcoding all the os related path separation (\\) so that your program can run on both Linux and Windows.

halflings
  • 1,540
  • 1
  • 13
  • 34