[Tutor] Open file error

Python python at venix.com
Tue Jan 17 19:29:01 CET 2006

(replying back to the list also)
On Tue, 2006-01-17 at 10:03 -0800, andy senoaji wrote:
> Sorry for the inconsistent error message. I think I may pasted
> incorretcly. I am now in a different machine, and have tested Paul's
> suggestion, and it worked. But would the 'r' tackles the escape
> sequence? I may misunderstood the intent of the r' switcher here. 
> Thanks,
> Andy
> f = open(r'C:\Test.txt', 'r')
>         This looks correct!

r'C:\Test.txt' is a raw string.  The r'strings' ignore the special use
of the backslash character except that a raw string must not end with a
backslash.  This is strictly a convenience when entering a string.
Since Windows uses backslash as the path separator, it helps ease the
pain when constructing file paths in Windows.  Raw strings are also very
helpful when writing regular expression strings which often need to have
backspace characters.

Internally, a raw string is the same as any other Python string.  When
Python displays a raw string, Python will show the backslashes as \\
(doubled) because that is usually how you need to enter a backslash.
Python does not track (as far as I know) that the string originated as a
raw string.

In regular strings, the backslash is used to mark characters that need
special handling. '\t' is a tab character. '\n' is a newline (linefeed).
'\r' is a carriage-return (Enter Key).

provides the detailed documentation about how strings work and the
definitive list of backspace usages.

> On 1/17/06, Python <python at venix.com> wrote:
>         On Tue, 2006-01-17 at 09:11 -0800, andy senoaji wrote:
>         > I am starting to pull my hair here. There were some postings
>         in the
>         > past, similar to my problem, but the response was not clear
>         enough.
>         > Sorry if you thingk I am reposting this. 
>         >
>         > I am trying to run (on an XP box) a simple open file using
>         this:
>         > f = open(r'C:\Test.txt', 'r')
>         This looks correct!
>         >
>         > but it keeps give me nagging error of:
>         > Traceback (most recent call last): 
>         >   File "<pyshell#0>", line 1, in -toplevel-
>         >     f = open('Test.txt', 'r')
>         This is not the same is the line above!
>         > IOError: [Errno 2] No such file or directory: 'C:\Test.txt' 
>         And this does not match *either* of the lines above.
>         If you really use that first line, I would expect it to
>         work.  If you
>         get an error, from that line, the file will be identified as:
>                 'C:\\Test.txt' 
>         >
>         > I know for sure that the file is there, I even put copies of
>         the files
>         > here and there, just to guess how python does the file
>         search, but it
>         > keeps giving me 'No such file or directory'. i also tried
>         variation of 
>         > the file location string, but gave me a variation of
>         errors :). Any
>         > suggestions?
>         >
>         > Furthermore, how does Python assumes the search path? Will
>         it look
>         > at /LIB first? How does it knows drive lettering, network
>         mapping etc? 
>         > Is there a configuration settings that I can tweak in my
>         Python? FYI I
>         > am using Activestate's.
>         >
>         >
>         > Thx,
>         >
>         > Andy
>         > _______________________________________________
>         > Tutor maillist  -  Tutor at python.org
>         > http://mail.python.org/mailman/listinfo/tutor
>         --
>         Lloyd Kvam
>         Venix Corp 

Lloyd Kvam
Venix Corp

More information about the Tutor mailing list