Case sensitive file names
Delaney, Timothy C (Timothy)
tdelaney at avaya.com
Sun Feb 22 21:24:19 EST 2004
> From: Thomas Philips
>
> Here's my example
> >>> file =open("c:\python23\programs\test.txt")
>
> Traceback (most recent call last):
> File "<pyshell#0>", line 1, in -toplevel-
> file =open("c:\python23\programs\test.txt")
> IOError: [Errno 2] No such file or directory:
> 'c:\\python23\\programs\test.txt'
>
> >>> file =open("c:\Python23\Programs\test.txt")
>
> Traceback (most recent call last):
> File "<pyshell#1>", line 1, in -toplevel-
> file =open("c:\Python23\Programs\test.txt")
> IOError: [Errno 2] No such file or directory:
> 'c:\\Python23\\Programs\test.txt'
>
> >>> file =open("c:\Python23\Programs\Test.txt")
> >>> print file.read()
> 1
> 2
> 3
You're not doing what you think you are doing.
A backslash in python (like many languages) is the escape character in strings. If you look at (for example)
> 'c:\\python23\\programs\test.txt'
you will notice that the first two backslashes are doubled (escaped backslash) but the second isn't.
The reason is that '\t' is the escape sequence for a tab character.
You need to do one of three things:
1. Escape all backslashes e.g. open(r"c:\\python23\\programs\\test.txt");
2. Use forward slashes (they will work on windows file paths through Python unless you are passing them to an external process) e.g. open("c:/python23/programs/test.txt")
3. Use raw strings e.g. open(r"c:\python23\programs\test.txt")
Tim Delaney
More information about the Python-list
mailing list