Rolf
Rolf

Reputation: 11

I am opening a file for writing but the file name I have chosen creates an error 22:

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

Upvotes: 0

Views: 49

Answers (1)

Rolf
Rolf

Reputation: 11

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.

Upvotes: 1

Related Questions