open file with whitespaces

M

mardif

Hi guys.
I've a very big big big problem:

I've in my windows computer a file named cicciobello.html, located in
c:\documents and settings\username\desktop\cicciobello.html.

Now, I MUST open this file with os.spawn(os.P_WAIT ...., because I must
wait the user cancel the explorer window, ok?
And so, I write:

import os
os.spawnl(os.P_WAIT, "c:\programmi\internet explorer\iexplorer.exe",
"c:\documents and settings\username\desktop\cicciobello.html")

the python process don't fail, but explorer don't visualize correctly
the file opened: i receive an "not found" error message.

I've found that the whitespaces in file path are the problem.
If you see to explorer address bar, you will find the address
completely wrong...

I've found no solution!
Can you help me, please??

thx very very very much!!!
 
C

Christophe

mardif a écrit :
Hi guys.
I've a very big big big problem:

I've in my windows computer a file named cicciobello.html, located in
c:\documents and settings\username\desktop\cicciobello.html.

Now, I MUST open this file with os.spawn(os.P_WAIT ...., because I must
wait the user cancel the explorer window, ok?
And so, I write:

import os
os.spawnl(os.P_WAIT, "c:\programmi\internet explorer\iexplorer.exe",
"c:\documents and settings\username\desktop\cicciobello.html")

the python process don't fail, but explorer don't visualize correctly
the file opened: i receive an "not found" error message.

I've found that the whitespaces in file path are the problem.
If you see to explorer address bar, you will find the address
completely wrong...

I've found no solution!
Can you help me, please??

thx very very very much!!!

And I thought the problem where the incorrectly used \ :) Try that first :

os.spawnl(os.P_WAIT, r"c:\programmi\internet explorer\iexplorer.exe",
r"c:\documents and settings\username\desktop\cicciobello.html")
 
B

bruno at modulix

mardif said:
Hi guys.
I've a very big big big problem:

<ot>
I think a lot of people in the world would not find it so big wrt/ their
own situation...
I've in my windows computer a file named cicciobello.html, located in
c:\documents and settings\username\desktop\cicciobello.html.

Now, I MUST open this file with os.spawn(os.P_WAIT ...., because I must
wait the user cancel the explorer window, ok?
And so, I write:

import os
os.spawnl(os.P_WAIT, "c:\programmi\internet explorer\iexplorer.exe",
"c:\documents and settings\username\desktop\cicciobello.html")

take care of the meaning of the antislash in strings... What do you
thing "c:\toto" or "c:\nothing" will expand to ?-)

import os.path
help(os.path)

Or at least, and IIRC, "c:/something/" should work.
the python process don't fail, but explorer don't visualize correctly
the file opened: i receive an "not found" error message.

I've found that the whitespaces in file path are the problem.
If you see to explorer address bar, you will find the address
completely wrong...

Not using Windows, I won't find anything. So what's the result ?-)

Anyway, you may find urllib.urlencode useful for, well, encoding urls...

HTH
 
C

Claudio Grondi

Christophe said:
mardif a écrit :


And I thought the problem where the incorrectly used \ :) Try that first :

os.spawnl(os.P_WAIT, r"c:\programmi\internet explorer\iexplorer.exe",
r"c:\documents and settings\username\desktop\cicciobello.html")
Another option to try in case this is not the problem is to put the file
name in quotation marks as it is necessary when using command line shell
(not tested, just an idea):

os.spawnl(os.P_WAIT,'"c:\\programmi\\internet explorer\\iexplorer.exe"',
'"c:\\documents and settings\\username\\desktop\\cicciobello.html"')
 
M

mardif

OK OK GUYS!!!!
I've found the solution: ( effectly, a friend of mine has found the
solution )

import os

os.spawnl(os.P_WAIT, "c:\programmi\internet
explorer\iexplore.exe",'"C:\Documents and
Settings\michele\Desktop\ciccio.html"','"C:\Documents and
Settings\michele\Desktop\ciccio.html"')

The secret are the ' simbols around arguments:

' "C:\Documents and Settings\michele\Desktop\ciccio.html" '

Without these, don't work!

Very STRONG!!!!!

bye and thx
 
F

Fredrik Lundh

mardif said:
Now, I MUST open this file with os.spawn(os.P_WAIT ...., because I must
wait the user cancel the explorer window, ok?

note that backslashes in string literals have special meaning in Python; to make
sure a backslash in the string literal really ends up as a backslash in the resulting
string, you must either double each backslash, or use "raw" strings:

program = r"c:\programmi\internet explorer\iexplorer.exe"
file = r"c:\documents and settings\username\desktop\cicciobello.html"

also, getting the quoting/escaping right with os.exec/os.spawn is quite messy. I
recommend using the subprocess module instead:

import subprocess
print subprocess.call([program, file])

</F>
 
C

Claudio Grondi

mardif said:
OK OK GUYS!!!!
I've found the solution: ( effectly, a friend of mine has found the
solution )

import os

os.spawnl(os.P_WAIT, "c:\programmi\internet
explorer\iexplore.exe",'"C:\Documents and
Settings\michele\Desktop\ciccio.html"','"C:\Documents and
Settings\michele\Desktop\ciccio.html"')

The secret are the ' simbols around arguments:

' "C:\Documents and Settings\michele\Desktop\ciccio.html" '

Without these, don't work!

Very STRONG!!!!!

bye and thx
Wasn't that what I have suggested?

By the way:
it is much better to use double backslashes here, as in case of other
file/directory names (e.g. folder\name.txt folder\remote.htm i.e. for
all the cases a backslash is followed by a letter making out of this
twin one special character) this above won't work.
But best is to use in full path file names forward slashes as Python
handles them properly not only on *nix and Linux, but also on Windows:
'"C:/Documents and Settings/michele/Desktop/ciccio.html"'

The "secret" symbols around arguments are single quotation marks making
the double quotation marks part of the string passed to the .spawnl()
function. Check out in IDLE, that:'"x"'
Command shell command arguments need to be enclosed in quotation marks
in case spaces can occur in them (as it can happen in file names) as
otherwise the spaces will be interpreted as separator characters between
the arguments and as consequence of this the command shell command fails
due to bad or wrong number of parameter.

You see, no "secrets" here ... only some simple rules and a bit more
understanding of what is going on behind the scenes.

Claudio
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top