Python doesn't see the directories I create

Steve Holden steve at holdenweb.com
Thu Aug 30 06:59:28 EDT 2007


Bruno Desthuilliers wrote:
> mr_gadget a écrit :
>> When I create a subfolder, python is not seeing it. Can someone please 
>> explain this behaviour ? I just started with python, read the tutorial over 
>> the weekend and am writing my very first script. So I may not be seeing 
>> something. Both os.path and glob.glob seem not to see a folder I created. 
>> Other sibling folders seem to work fine. On a whim I tried paths with \\ 
>> double slashes and that worked. But why should single slashes work for some 
>> folders and not for others ??
> 
Note you *didn't* try paths with double slashes, you merely correctly 
represented the paths with single slashes :-)

> s/slash/antislash/g
> 
> It's a very well known gotcha due to MS's choice to use the antislash as 
> path separator. In most languages - Python included - the antislash is 
> used for escape sequences (non-printable characters). '\r' is the escape 
> sequence for CR (carriage return). Doubling the antislash prevents 
> escaping.
> 
> You can avoid all escaping by using raw strings:
> 
> mypath = r"C:\enhancement\rawfiles\"
> 
Please note that the above is a well-known syntax error. A string 
literal cannot end with a single backslash, as it escapes the closing quote.

 >>> mypath = r"C:\enhancement\rawfiles\"
   File "<stdin>", line 1
     mypath = r"C:\enhancement\rawfiles\"
                                        ^
SyntaxError: EOL while scanning single-quoted string
 >>>

> Also and IIRC, using slash instead should also work, ie:
> 
> mypath = r"C:/enhancement/rawfiles/"
> 
That does indeed work in most situations, but ideally (i.e. for maximum 
code portability) paths should be constructed using os.path.join(), or 
collected from the environment somehow.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list