open filename with spaces in path

  • Thread starter Michael Robertson
  • Start date
M

Michael Robertson

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
Exception

but yet...

works just fine.
 
C

Christian Heimes

Michael said:
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

Exception

Works for me
<open file './my test/test.txt', mode 'r' at 0xb7dd6a40>

Christian
 
G

Gary Herron

Michael said:
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

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
 
K

Kam-Hung Soh

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

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


works just fine.

Couldn't test on Linux, but in Windows ...
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'
 
K

Kam-Hung Soh

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

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


works just fine.

Couldn't test on Linux, but in Windows ...
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:
 
C

Chris

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

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'
[/QUOTE]
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''C:\\temp\\my test'

Windoze works fine if you hand it forward slashes.
'C:\\temp\\my test'

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

Chris

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. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top