469,644 Members | 1,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

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.
Jun 27 '08 #1
8 33160
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

Jun 27 '08 #2
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 | md*@MicahElliott.com | http://MicahElliott.blogspot.com
Jun 27 '08 #3
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
Jun 27 '08 #4
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
<mc*********@hotmail.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 "<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>
Jun 27 '08 #5
On Wed, 07 May 2008 09:09:08 +1000, Kam-Hung Soh <ka*********@gmail.com>
wrote:
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
<mc*********@hotmail.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 "<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>
Jun 27 '08 #6
On May 7, 1:09*am, "Kam-Hung Soh" <kamhung....@gmail.comwrote:
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson *

<mcrobert...@hotmail.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:\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.
Jun 27 '08 #7
On 2008-05-07, Chris <cw****@gmail.comwrote:
>>>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
Jun 27 '08 #8
On May 7, 3:25*pm, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
On 2008-05-07, Chris <cwi...@gmail.comwrote:
>>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. ;)
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rob Cowie | last post: by
1 post views Thread by Thomas Anderson | last post: by
8 posts views Thread by PvtPile | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.