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

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: ‘pathregionlog.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('pathregionlog.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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use forward slashes:

'path/regionlog.txt'

Or raw strings:

r'pathregionlog.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'))

Method 2

In C standard language, t, n, r are escape characters. t is a transverse to the next TAB position. n is a newline and r is a carriage return. You should use \r or /r, and you will solve the problem!

Method 3

Additionaly, Python also gives this message when trying to open a file > 50 MB from a SharePoint shared drive.

https://support.microsoft.com/en-us/help/2668751/you-cannot-download-more-than-50-mb-or-upload-large-files-when-the-upl

Method 4

Another simple solution is changing the “r” instances in the filename path to “R”


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x