Double backslash in filepaths ?

Discussion in 'Python' started by Stef Mientki, Apr 14, 2007.

  1. Stef Mientki

    Stef Mientki Guest

    It looks like sometimes a single backslash is replaced by a double backslash,
    but sometimes it's not ???
    See the error message below,
    the first backslash is somewhere (not explicitly in my code) replaced,
    but the second is not ???
    Is it in general better to use double backslash in filepaths ?

    thanks,
    Stef Mientki


    >>> Write_Signal_File_Ext (IOO, fSamp, 'D:\data_to_test\test_global.pd')

    Traceback (most recent call last):
    File "<string>", line 21, in ?
    File "D:\data_to_test\Signal_WorkBench.py", line 118, in Write_Signal_File_Ext
    DataFile.Write_Data (Data)
    File "D:\data_to_test\Signal_WorkBench.py", line 95, in Write_Data
    self.Write_Header(Nchan)
    File "D:\data_to_test\Signal_WorkBench.py", line 83, in Write_Header
    self.datafile = open(self.filename,'wb')
    IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'
     
    Stef Mientki, Apr 14, 2007
    #1
    1. Advertising

  2. On Apr 14, 2007, at 4:26 AM, Stef Mientki wrote:

    > It looks like sometimes a single backslash is replaced by a double
    > backslash,
    > but sometimes it's not ???
    > See the error message below,
    > the first backslash is somewhere (not explicitly in my code) replaced,
    > but the second is not ???
    > Is it in general better to use double backslash in filepaths ?
    >
    > thanks,
    > Stef Mientki
    >
    >
    >>>> Write_Signal_File_Ext (IOO, fSamp, 'D:\data_to_test
    >>>> \test_global.pd')

    > Traceback (most recent call last):
    > File "<string>", line 21, in ?
    > File "D:\data_to_test\Signal_WorkBench.py", line 118, in
    > Write_Signal_File_Ext
    > DataFile.Write_Data (Data)
    > File "D:\data_to_test\Signal_WorkBench.py", line 95, in
    > Write_Data
    > self.Write_Header(Nchan)
    > File "D:\data_to_test\Signal_WorkBench.py", line 83, in
    > Write_Header
    > self.datafile = open(self.filename,'wb')
    > IOError: [Errno 2] No such file or directory: 'D:\\data_to_test
    > \test_global.pd'


    If I remember correctly, you don't really have to use backslashes at
    all. I think that's just a holdover from when DOS filesystems first
    became hierarchical -- and they had already used the sensible
    directory delimiter '/' as a command line switch character. So I
    think you can just substitute forward slashes and forget that double-
    backslash madness.

    But that's not really answering your question, is it?

    What you're looking for is called 'escape characters'. The single
    backslash combines with the 't' to become a TAB character. The
    double backslashes combine to become '\'. So:

    >>> print 'D:\\data_to_test\test_global.pd'

    D:\data_to_test est_global.pd

    hth,
    Michael
     
    Michael Bentley, Apr 14, 2007
    #2
    1. Advertising

  3. Stef Mientki

    John J. Lee Guest

    Stef Mientki <> writes:

    > It looks like sometimes a single backslash is replaced by a double backslash,
    > but sometimes it's not ???
    > See the error message below,
    > the first backslash is somewhere (not explicitly in my code) replaced,
    > but the second is not ???
    > Is it in general better to use double backslash in filepaths ?

    [...]
    > IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'


    '\t' is the tab character. '\d' is not a valid escape sequence, so
    the backslash there survives intact. repr() normalises the string on
    output, so the (single!) backslash in '\d' is displayed, as always in
    the repr of strings, as '\\'.

    You should either use this:

    'D:\\data_to_test\\test_global.pd'


    Or this:

    r'D:\data_to_test\test_global.pd'

    See also:

    http://www.python.org/doc/faq/general/#why-can-t-raw-strings-r-strings-end-with-a-backslash


    Or even this, which will work unless you're using crufty software that
    doesn't like slash path separators (cmd.exe being one of those pieces
    of software):

    'D:/data_to_test/test_global.pd'


    John
     
    John J. Lee, Apr 14, 2007
    #3
  4. Stef Mientki

    Stef Mientki Guest

    John J. Lee wrote:
    > Stef Mientki <> writes:
    >
    >> It looks like sometimes a single backslash is replaced by a double backslash,
    >> but sometimes it's not ???
    >> See the error message below,
    >> the first backslash is somewhere (not explicitly in my code) replaced,
    >> but the second is not ???
    >> Is it in general better to use double backslash in filepaths ?

    > [...]
    >> IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'

    >
    > '\t' is the tab character. '\d' is not a valid escape sequence, so
    > the backslash there survives intact. repr() normalises the string on
    > output, so the (single!) backslash in '\d' is displayed, as always in
    > the repr of strings, as '\\'.
    >
    > You should either use this:
    >
    > 'D:\\data_to_test\\test_global.pd'
    >
    >
    > Or this:
    >
    > r'D:\data_to_test\test_global.pd'
    >
    > See also:
    >
    > http://www.python.org/doc/faq/general/#why-can-t-raw-strings-r-strings-end-with-a-backslash
    >
    >
    > Or even this, which will work unless you're using crufty software that
    > doesn't like slash path separators (cmd.exe being one of those pieces
    > of software):
    >
    > 'D:/data_to_test/test_global.pd'
    >
    >
    > John


    thanks John and Michael,
    this is a very helpful explanation.

    cheers,
    Stef
     
    Stef Mientki, Apr 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomas
    Replies:
    5
    Views:
    13,829
    Roedy Green
    Sep 25, 2003
  2. Vitali Gontsharuk
    Replies:
    2
    Views:
    1,702
    Vitali Gontsharuk
    Aug 21, 2005
  3. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  4. Vincent Texier
    Replies:
    3
    Views:
    2,414
    Vincent Texier
    Jul 1, 2004
  5. Stef Mientki
    Replies:
    31
    Views:
    750
    Paul McNett
    Oct 24, 2008
Loading...

Share This Page