os.path.normpath question

Duncan Booth duncan.booth at invalid.invalid
Tue Jun 8 06:43:56 EDT 2010


Bart <xc68000 at gmail.com> wrote:

> I'm using this and ran across backslash issues in one of my paths.
> 
>      archpath = os.path.normpath('E:\foo\FTP\HLS\archive')
> 
> was translating to:
> 
>      E:\lsfprod\law\uch_interfaces\FTP\HLSrchive
> 
> which caused me to start using the 'raw' declaration before the path
> string like this:
> 
>      archpath = os.path.normpath(r'E:\foo\FTP\HLS\archive')
> 
> Is this the right way to use this?
> 

If you want to use raw strings for Windows paths then you need to be aware 
that you cannot end a raw string with a backslash.

In most cases you can just use forward slashes instead and then you don't 
need to worry about escaping them:

    archpath = os.path.normpath('E:/foo/FTP/HLS/archive')


Or better still, don't put hardwired paths into your application, put them 
all in a config file.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list