A silly question on file opening

Anthony Tolle anthony.tolle at gmail.com
Wed Feb 10 15:57:52 EST 2010


On Feb 10, 3:42 pm, joy99 <subhakolkata1... at gmail.com> wrote:
> Dear Group,
> [snip]
> I tried to change the location to D:\file and as I saw in Python Docs
> the file reading option is now "r+" so I changed the statement to
>    file_open=open("D:\file","r+")
> but it is still giving error.

Only use "r+" if you need to also write to the file.  "r" is still
good for opening for reading.

Without seeing a traceback, I can only assume the error is caused by
using a backslash in the path without escaping it.  Try either the
following:

file_open=open("D:\\file","r+")

file_open=open(r"D:\file","r+")

For an explanation, see the Python documentation:

http://docs.python.org/reference/lexical_analysis.html#string-literals



More information about the Python-list mailing list