open filename with spaces in path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Robertson

    open filename with spaces in path

    I'm having trouble opening a file in linux, whose path has spaces in it.

    $ mkdir my\ test
    $ echo test my\ test/test.txt
    $ python
    >>open('./my test/test.txt')
    Exception
    >>open('./my\\ test/test.txt')
    Exception

    but yet...
    >>import os
    >>os.chdir('./my test')
    >>open('./test')
    works just fine.
  • Christian Heimes

    #2
    Re: open filename with spaces in path

    Michael Robertson schrieb:
    I'm having trouble opening a file in linux, whose path has spaces in it.
    >
    $ mkdir my\ test
    $ echo test my\ test/test.txt
    $ python
    >
    >>>open('./my test/test.txt')
    Exception
    Works for me
    >>open('./my test/test.txt')
    <open file './my test/test.txt', mode 'r' at 0xb7dd6a40>

    Christian

    Comment

    • Micah Elliott

      #3
      Re: open filename with spaces in path

      On 2008-05-06 Michael Robertson wrote:
      I'm having trouble opening a file in linux, whose path has
      spaces in it.
      >
      $ mkdir my\ test
      $ echo test my\ test/test.txt
      $ python
      >
      >open('./my test/test.txt')
      Exception
      That's funny. These exact steps work fine for me on Linux, with
      Python 2.5.2, and even 1.5.2.

      Does 'os.path.normpa th' have anything interesting to say about
      what you're passing it?
      but yet...
      >
      >import os
      >os.chdir('./my test')
      >open('./test')
      >
      works just fine.
      How doctored up is this example? In the above ./test should not
      actually exist. Did the chdir actually work? Or did you remove
      the .txt?

      --
      Micah Elliott | mde@MicahElliot t.com | http://MicahElliott.blogspot.com

      Comment

      • Gary Herron

        #4
        Re: open filename with spaces in path

        Michael Robertson wrote:
        I'm having trouble opening a file in linux, whose path has spaces in it.
        >
        $ mkdir my\ test
        $ echo test my\ test/test.txt
        $ python
        >
        >open('./my test/test.txt')
        Exception
        This works just fine for me. No need to escape the spaces.

        You haven't given us much to work with -- tell us what exception you
        get. (Or just look at it carefully yourself.) Whatever the problem
        is, I don't believe it has anything to do with the spaces.

        Gary Herron

        >
        >open('./my\\ test/test.txt')
        Exception
        but yet...
        >
        >import os
        >os.chdir('./my test')
        >open('./test')
        >
        works just fine.
        --
        http://mail.python.org/mailman/listinfo/python-list

        Comment

        • Kam-Hung Soh

          #5
          Re: open filename with spaces in path

          On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
          <mcrobertson@ho tmail.comwrote:
          I'm having trouble opening a file in linux, whose path has spaces in it.
          >
          $ mkdir my\ test
          $ echo test my\ test/test.txt
          $ python
          >
          >>open('./my test/test.txt')
          Exception
          >>open('./my\\ test/test.txt')
          Exception
          >
          Try a string literal by prefixing your path string with "r":

          open(r'./my test/test.txt')

          See http://docs.python.org/ref/strings.html
          but yet...
          >
          >>import os
          >>os.chdir('./my test')
          >>open('./test')
          >
          works just fine.
          Couldn't test on Linux, but in Windows ...
          >>os.chdir('C:\ temp\my test')
          Traceback (most recent call last):
          File "<interacti ve input>", line 1, in <module>
          WindowsError: [Error 123] The filename, directory name, or volume label
          syntax is incorrect: 'C:\temp\\my test'

          --
          Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

          Comment

          • Kam-Hung Soh

            #6
            Re: open filename with spaces in path

            On Wed, 07 May 2008 09:09:08 +1000, Kam-Hung Soh <kamhung.soh@gm ail.com>
            wrote:
            On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
            <mcrobertson@ho tmail.comwrote:
            >
            >I'm having trouble opening a file in linux, whose path has spaces in it.
            >>
            >$ mkdir my\ test
            >$ echo test my\ test/test.txt
            >$ python
            >>
            > >>open('./my test/test.txt')
            >Exception
            > >>open('./my\\ test/test.txt')
            >Exception
            >>
            >
            Try a string literal by prefixing your path string with "r":
            >
            open(r'./my test/test.txt')
            >
            See http://docs.python.org/ref/strings.html
            >
            >but yet...
            >>
            > >>import os
            > >>os.chdir('./my test')
            > >>open('./test')
            >>
            >works just fine.
            >
            Couldn't test on Linux, but in Windows ...
            >
            >>>os.chdir('C: \temp\my test')
            Traceback (most recent call last):
            File "<interacti ve input>", line 1, in <module>
            WindowsError: [Error 123] The filename, directory name, or volume label
            syntax is incorrect: 'C:\temp\\my test'
            >
            Oops. Should have been:
            >>os.chdir(r'C: \temp\my test')
            >>(no errors)
            --
            Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

            Comment

            • Chris

              #7
              Re: open filename with spaces in path

              On May 7, 1:09 am, "Kam-Hung Soh" <kamhung....@gm ail.comwrote:
              On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson  
              >
              <mcrobert...@ho tmail.comwrote:
              I'm having trouble opening a file in linux, whose path has spaces in it.
              >
              $ mkdir my\ test
              $ echo test my\ test/test.txt
              $ python
              >
               >>open('./my test/test.txt')
              Exception
               >>open('./my\\ test/test.txt')
              Exception
              >
              Try a string literal by prefixing your path string with "r":
              >
              open(r'./my test/test.txt')
              >
              Seehttp://docs.python.org/ref/strings.html
              >
              but yet...
              >
               >>import os
               >>os.chdir('./my test')
               >>open('./test')
              >
              works just fine.
              >
              Couldn't test on Linux, but in Windows ...
              >
              >os.chdir('C:\t emp\my test')
              >
              Traceback (most recent call last):
                 File "<interacti ve input>", line 1, in <module>
              WindowsError: [Error 123] The filename, directory name, or volume label  
              syntax is incorrect: 'C:\temp\\my test'
              >
              --
              Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
              >>os.chdir('C :/')
              >>os.getcwd()
              'C:\\'
              >>os.chdir('C:\ temp\my test')
              Traceback (most recent call last):
              File "<input>", line 1, in <module>
              WindowsError: [Error 123] The filename, directory name, or volume
              label syntax is incorrect: 'C:\temp\\my test'
              >>os.chdir('C :/temp/my test')
              >>os.getcwd()
              'C:\\temp\\my test'

              Windoze works fine if you hand it forward slashes.
              >>os.chdir('C :/')
              >>os.chdir(os.p ath.join('C:',' temp','my test'))
              >>os.getcwd()
              'C:\\temp\\my test'

              And joining your target with os.path.join works to.

              Comment

              • A.T.Hofkamp

                #8
                Re: open filename with spaces in path

                On 2008-05-07, Chris <cwitts@gmail.c omwrote:
                >>>os.chdir('C: \temp\my test')
                Traceback (most recent call last):
                File "<input>", line 1, in <module>
                WindowsError: [Error 123] The filename, directory name, or volume
                label syntax is incorrect: 'C:\temp\\my test'
                Python strings have \ escapes, such as \t, \n etc. Above you try to go to

                C:<TAB>emp\my test

                and you are lucky that \m is not something special.


                Either escape your back slashes ("C:\\temp\\ my test") or use raw strings
                (r"C:\temp\my test").

                Sincerely,
                Albert

                Comment

                • Chris

                  #9
                  Re: open filename with spaces in path

                  On May 7, 3:25 pm, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
                  On 2008-05-07, Chris <cwi...@gmail.c omwrote:
                  >
                  >>os.chdir('C:\ temp\my test')
                  Traceback (most recent call last):
                    File "<input>", line 1, in <module>
                  WindowsError: [Error 123] The filename, directory name, or volume
                  label syntax is incorrect: 'C:\temp\\my test'
                  >
                  Python strings have \ escapes, such as \t, \n etc. Above you try to go to
                  >
                  C:<TAB>emp\my test
                  >
                  and you are lucky that \m is not something special.
                  >
                  Either escape your back slashes ("C:\\temp\\ my test") or use raw strings
                  (r"C:\temp\my test").
                  >
                  Sincerely,
                  Albert
                  My personal preference is using backslashes, if it's something quick
                  or if it's something that will be sitting on a server in a dark corner
                  for a long time to just use os.path.join. I've had neither of those 2
                  break on me. ;)

                  Comment

                  Working...