0

if I run this code I get the error below: fout = open ('M:\projects\EGU\BS\bsofab.txt', 'w')

IOError: [Errno 22] invalid mode ('w') or filename: 'M:\projects\EGU\BS\x08sofab.txt'

If I change the file name to ('M:\projects\EGU\BS\ofbsab.txt', 'w') it work fine. can someone please tell me what is going on?

thanks

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Rolf
  • 11
  • 1

1 Answers1

1

I work in windows only and I have discovered that in a UNIX or Mac environment filenames use forward slashes for file paths and back slashes are used for “escape characters’. \b means “backspace”.

by adding the r to the path like this(r'M:\projects\EGU\BS\bsofab.txt', 'w’) it works perfectly.

r is for "raw" and essentially allows the last backslash to be ignored.

I did find some more complicated solutions but this was by far the easiest to implement.

Rolf
  • 11
  • 1
  • Not just the last backslash, *all* backslashes. Since there's hardly ever a reason to use escaped characters in a filename, a raw string is the perfect solution. But you might be surprised to learn that forward slashes work in Windows too. – Mark Ransom May 24 '16 at 20:26
  • Thanks for the clarification! – Rolf May 26 '16 at 20:22