IOerror : need urgent help

MRAB python at mrabarnett.plus.com
Tue Feb 19 16:54:39 CET 2013


On 2013-02-19 15:27, inshu chauhan wrote:
> Here is my attempt to merge 10 files stored in a folder into a single file :
>
> import csv
>
> with open("C:\Users\inshu.chauhan\Desktop\test.arff", "w") as w:
>      writer = csv.writer(w)
>      for f in glob.glob("C:\Users\inshu.chauhan\Desktop\For
> Model_600\*.arff"):
>          rows = open(f, "r").readlines()
>          writer.writerows(rows)
>
>
> Error:
>
> Traceback (most recent call last):
>    File "C:\Users\inshu.chauhan\Desktop\Mergefiles.py", line 3, in <module>
>      with open("C:\Users\inshu.chauhan\Desktop\test.arff", "w") as w:
> IOError: [Errno 22] invalid mode ('w') or filename:
> 'C:\\Users\\inshu.chauhan\\Desktop\test.arff'
>
> Why my programme is not working ?? :(
>
Look at the traceback. It says that the path is:

'C:\\Users\\inshu.chauhan\\Desktop\test.arff'

All but one of the backslashes are doubled.

That's because the backslash character \ starts an escape sequence, but
if it can't recognise the escape sequence, it treats the backslash as a
literal character.

In that string literal, '\t' is an escape sequence representing a tab
character (it's equal to chr(9)), but '\U', '\i' and '\D' are not
escape sequences, so they are equivalent to '\\U, '\\i' and '\\D'
respectively.

What you should do is use raw string literals for paths:

r"C:\Users\inshu.chauhan\Desktop\test.arff"

or use '/' instead (Windows allows it as an alternative, unless it
occurs initially, which you'll rarely want to do in practice):

"C:/Users/inshu.chauhan/Desktop/test.arff"




More information about the Python-list mailing list