Double backslash in filepaths ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    #1

    Double backslash in filepaths ?

    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_tes t\test_global.p d')
    Traceback (most recent call last):
    File "<string>", line 21, in ?
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 118, in Write_Signal_Fi le_Ext
    DataFile.Write_ Data (Data)
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 95, in Write_Data
    self.Write_Head er(Nchan)
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 83, in Write_Header
    self.datafile = open(self.filen ame,'wb')
    IOError: [Errno 2] No such file or directory: 'D:\\data_to_te st\test_global. pd'
  • Michael Bentley

    #2
    Re: Double backslash in filepaths ?


    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_tes t
    >>>\test_global .pd')
    Traceback (most recent call last):
    File "<string>", line 21, in ?
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 118, in
    Write_Signal_Fi le_Ext
    DataFile.Write_ Data (Data)
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 95, in
    Write_Data
    self.Write_Head er(Nchan)
    File "D:\data_to_tes t\Signal_WorkBe nch.py", line 83, in
    Write_Header
    self.datafile = open(self.filen ame,'wb')
    IOError: [Errno 2] No such file or directory: 'D:\\data_to_te st
    \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_te st\test_global. pd'
    D:\data_to_test est_global.pd

    hth,
    Michael

    Comment

    • John J. Lee

      #3
      Re: Double backslash in filepaths ?

      Stef Mientki <S.Mientki-nospam@mailbox. kun.nlwrites:
      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_te st\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_te st\\test_global .pd'


      Or this:

      r'D:\data_to_te st\test_global. pd'

      See also:

      Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...



      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

      Comment

      • Stef Mientki

        #4
        Re: Double backslash in filepaths ?

        John J. Lee wrote:
        Stef Mientki <S.Mientki-nospam@mailbox. kun.nlwrites:
        >
        >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_te st\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_te st\\test_global .pd'
        >
        >
        Or this:
        >
        r'D:\data_to_te st\test_global. pd'
        >
        See also:
        >
        Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...

        >
        >
        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

        Comment

        Working...