Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Region: IOError: [Errno 22] invalid mode ('w') or filename

Tags:

I'm not sure why, but for some reason, whenever I have "region" in the file name of the output file, it gives me this error:

IOError: [Errno 22] invalid mode ('w') or filename: 'path\regionlog.txt'

It does this for "region.txt", "logregion.txt", etc.

class writeTo:     def __init__(self, stdout, name):        self.stdout = stdout        self.log = file(name, 'w') #here is where it says the error occurs  output = os.path.abspath('path\regionlog.txt') writer = writeTo(sys.stdout, output) #and here too 

Why is this? I really would like to name my file "regionlog.txt" but it keeps coming up with that error. Is there a way around it?

like image 238
FaerieDrgn Avatar asked Feb 28 '13 17:02

FaerieDrgn


1 Answers

Use forward slashes:

'path/regionlog.txt' 

Or raw strings:

r'path\regionlog.txt' 

Or at least escape your backslashes:

'path\\regionlog.txt' 

\r is a carriage return.


Another option: use os.path.join and you won't have to worry about slashes at all:

output = os.path.abspath(os.path.join('path', 'regionlog.txt')) 
like image 147
Pavel Anossov Avatar answered Oct 06 '22 05:10

Pavel Anossov