0

Most of the times this part of the code works. But only some times it fails with the error message shown. I am wondering if this is a code problem?

crfile=strftime("%Y-%m-%d %H_%M_%S")
file = open("c:\\all-files\\%s.log" % crfile,"w")

IOError: [Errno 22] invalid mode ('w') or filename: 'c:\\all-files\\2015-03-30 19_05_09.log'
Omair .
  • 344
  • 2
  • 12
  • Sometimes it works, others it doesn't? – Zizouz212 Mar 31 '15 at 03:12
  • So, sometimes it runs days without having these errors. Sometimes, it takes hours for it to error out. – Omair . Mar 31 '15 at 03:17
  • Take a look at this: It my help you. I think that an error is popping up because there might be something to do with spaces in the filename: http://stackoverflow.com/questions/14852140/whitespaces-in-the-path-of-windows-filepath – Zizouz212 Mar 31 '15 at 03:17
  • What's with the double backslash? and **NEVER** name a variable `file` in Python – jkd Mar 31 '15 at 03:19
  • @jakekimds Double backslashes are used so that they don't interfere with other things, such as creating other things in the string that aren't wanted (like newline tokens). It's quite standard. And why *never*? – Zizouz212 Mar 31 '15 at 03:20
  • @Zizouz212 Sorry, didn't know. Used to Unix where they were used to escape spaces. – jkd Mar 31 '15 at 03:21
  • @jakekimds I use unix too :) – Zizouz212 Mar 31 '15 at 03:21
  • Wouldn't python read the string as `'c:\all-files\2015-03-30 19_05_09.log'` because they escape one another out? But how come the error keeps all the backslashes? – jkd Mar 31 '15 at 03:22
  • @jakekimds Yep, your right. That's how Python reads it. To figure it out, print `"\n"` out. Then, `"\ta"`. – Zizouz212 Mar 31 '15 at 03:23
  • I'm placing an answer that takes into summary what I had recommended. – Zizouz212 Mar 31 '15 at 03:24
  • @Omair. what happens when you replace backslashes with forward slashes? Completely legal. – jkd Mar 31 '15 at 03:34
  • @jakekimds I think backslashes are default on windows. They pop up automatically. – Zizouz212 Mar 31 '15 at 03:51

1 Answers1

0

The issue could be that you have spaces in your filename. Try changing it to a raw string.

path = r"My/Amazing Path.txt"

Otherwise, you might have some other issue. See this answer for more details.

Also, don't use file as a variable name. Generally, using python keywords as variables names aren't a good idea.

Community
  • 1
  • 1
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • He still shouldn't use `file` as a variable name – jkd Mar 31 '15 at 03:31
  • You should mention it – jkd Mar 31 '15 at 03:52
  • From what I have read, python does handle spaces properly. So, I don't think using raw string literals would make a difference. But, yes I will change "file" variable name. – Omair . Mar 31 '15 at 04:25
  • The variable name shouldn't be your problem, when you create the file, try closing it (even if it does give you an error) and can you check if the file is there in the directory manually? – Zizouz212 Mar 31 '15 at 14:52