Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39239

Re: IOerror : need urgent help

Date 2013-02-19 15:54 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: IOerror : need urgent help
References <CAFqGZREMOm4YSntzwie8o=RmusdV8KzrggY0wK8w4vCXWuwmBw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2037.1361289281.2939.python-list@python.org> (permalink)

Show all headers | View raw


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"

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: IOerror : need urgent help MRAB <python@mrabarnett.plus.com> - 2013-02-19 15:54 +0000

csiph-web