Quantcast

open filename with spaces in path

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

open filename with spaces in path

Michael Robertson-4
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.
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Christian Heimes-2
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

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Micah Elliott-3
In reply to this post by Michael Robertson-4
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.normpath' 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 | [hidden email] | http://MicahElliott.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Gary Herron
In reply to this post by Michael Robertson-4
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

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Kam-Hung Soh
In reply to this post by Michael Robertson-4
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson  
<[hidden email]> 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')

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 "<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>
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Kam-Hung Soh
On Wed, 07 May 2008 09:09:08 +1000, Kam-Hung Soh <[hidden email]>  
wrote:

> On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson  
> <[hidden email]> 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')
>
> 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 "<interactive 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>
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Chris-374
In reply to this post by Kam-Hung Soh
On May 7, 1:09 am, "Kam-Hung Soh" <[hidden email]> wrote:

> On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson  
>
> <[hidden email]> 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

A.T.Hofkamp-3
On 2008-05-07, Chris <[hidden email]> wrote:
>>>> 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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open filename with spaces in path

Chris-374
On May 7, 3:25 pm, "A.T.Hofkamp" <[hidden email]> wrote:

> On 2008-05-07, Chris <[hidden email]> wrote:
>
> >>>> 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. ;)
--
http://mail.python.org/mailman/listinfo/python-list
Loading...