On May 7, 1:09 am, "Kam-Hung Soh" <[EMAIL PROTECTED]> wrote:
> On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson  
>
> <[EMAIL PROTECTED]> 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
> >  >>> 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:\temp\my test')
>
> Traceback (most recent call last):
>    File "<interactive 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.path.join('C:','temp','my test'))
>>> os.getcwd()
'C:\\temp\\my test'

And joining your target with os.path.join works to.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to