[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Spaces in path name

David S

3/13/2008 10:17:00 PM

Hi,

I have some code in which I have to change some path names to get it to
work. The original code seems to have assumed that path names would not have
any embedded spaces.

I am not sure how to write the following line so when used in my script the
path will be found.

ANT_CMD = r'C:\Program Files\apache-ant-1.7.0\bin\ant'

Regards,
David


23 Answers

Mensanator

3/13/2008 11:33:00 PM

0

On Mar 13, 5:16 pm, "David S" <dav...@evertech.com.au> wrote:
> Hi,
>
> I have some code in which I have to change some path names to get it to
> work. The original code seems to have assumed that path names would not have
> any embedded spaces.
>
> I am not sure how to write the following line so when used in my script the
> path will be found.
>
> ANT_CMD  = r'C:\Program Files\apache-ant-1.7.0\bin\ant'

Try ANT_CMD = r'"C:\Program Files\apache-ant-1.7.0\bin\ant"'

>
> Regards,
> David

David S

3/14/2008 4:37:00 AM

0

Hi,

I get

ERROR: ""C:\Program Files\apache-ant-1.7.0\bin\ant"" does not exist

If I cut the path statement here and paste it next to a windows XP command
prompt ant is invoked.

The python code here is
if not os.path.isfile(ANT_CMD):
error('"%s" does not exist' % ANT_CMD)

David

"Mensanator" <mensanator@aol.com> wrote in message
news:360a8612-f5a2-4f2f-bae2-c89ed35b7d1b@n77g2000hse.googlegroups.com...
On Mar 13, 5:16 pm, "David S" <dav...@evertech.com.au> wrote:
> Hi,
>
> I have some code in which I have to change some path names to get it to
> work. The original code seems to have assumed that path names would not
> have
> any embedded spaces.
>
> I am not sure how to write the following line so when used in my script
> the
> path will be found.
>
> ANT_CMD = r'C:\Program Files\apache-ant-1.7.0\bin\ant'

Try ANT_CMD = r'"C:\Program Files\apache-ant-1.7.0\bin\ant"'

>
> Regards,
> David


Dennis Lee Bieber

3/14/2008 6:36:00 AM

0

On Fri, 14 Mar 2008 04:37:12 GMT, "David S" <davids@evertech.com.au>
declaimed the following in comp.lang.python:


> If I cut the path statement here and paste it next to a windows XP command
> prompt ant is invoked.
>
> The python code here is
> if not os.path.isfile(ANT_CMD):
> error('"%s" does not exist' % ANT_CMD)
>
Any chance the /filename/ is something like "ant.exe"

>>> pyt = "e:/python24/python"
>>> if not os.path.isfile(pyt):
.... print "%s does not exist" % pyt
....
e:/python24/python does not exist
>>> pyt = "e:/python24/python.exe"
>>> if not os.path.isfile(pyt):
.... print "%s does not exist" % pyt
....
>>>

Remember, under Windows, files with certain extensions are
executable /without/ needing the extension on the command line -- unlike
Linux...

My eclipse install has: ant.bat, ant.cmd, and ant.jar files, and
which (if they are in the same directory) gets picked when the extension
is not specified is beyond my current knowledge -- might depend on the
order of the extensions specified in the

C:\Documents and Settings\Dennis Lee Bieber>echo %pathext%
..COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.pyw;.py;.pyo;.pyc;.tcl

(okay, the ant.jar won't be picked <G>)

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

David S

3/14/2008 7:31:00 AM

0

Hi,

Using "C:\Program Files\apache-ant-1.7.0\bin\ant.bat" just gives me the same
result.

David

"Dennis Lee Bieber" <wlfraed@ix.netcom.com> wrote in message
news:13tk779i4r8j9ba@corp.supernews.com...
> On Fri, 14 Mar 2008 04:37:12 GMT, "David S" <davids@evertech.com.au>
> declaimed the following in comp.lang.python:
>
>
>> If I cut the path statement here and paste it next to a windows XP
>> command
>> prompt ant is invoked.
>>
>> The python code here is
>> if not os.path.isfile(ANT_CMD):
>> error('"%s" does not exist' % ANT_CMD)
>>
> Any chance the /filename/ is something like "ant.exe"
>
>>>> pyt = "e:/python24/python"
>>>> if not os.path.isfile(pyt):
> ... print "%s does not exist" % pyt
> ...
> e:/python24/python does not exist
>>>> pyt = "e:/python24/python.exe"
>>>> if not os.path.isfile(pyt):
> ... print "%s does not exist" % pyt
> ...
>>>>
>
> Remember, under Windows, files with certain extensions are
> executable /without/ needing the extension on the command line -- unlike
> Linux...
>
> My eclipse install has: ant.bat, ant.cmd, and ant.jar files, and
> which (if they are in the same directory) gets picked when the extension
> is not specified is beyond my current knowledge -- might depend on the
> order of the extensions specified in the
>
> C:\Documents and Settings\Dennis Lee Bieber>echo %pathext%
> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.pyw;.py;.pyo;.pyc;.tcl
>
> (okay, the ant.jar won't be picked <G>)
>
> --
> Wulfraed Dennis Lee Bieber KD6MOG
> wlfraed@ix.netcom.com wulfraed@bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-asst@bestiaria.com)
> HTTP://www.bestiaria.com/


Bryan Olson

3/14/2008 8:19:00 AM

0

David S wrote:
> I get
>
> ERROR: ""C:\Program Files\apache-ant-1.7.0\bin\ant"" does not exist
>
> If I cut the path statement here and paste it next to a windows XP command
> prompt ant is invoked.
>
> The python code here is
> if not os.path.isfile(ANT_CMD):
> error('"%s" does not exist' % ANT_CMD)

There you don't want the quotes within the string. On my MS-Win box:

>>> import os
>>> os.path.isfile(r'C:\Program Files\Windows NT\dialer.exe')
True
>>> print os.path.isfile(r'"C:\Program Files\Windows NT\dialer.exe"')
False


--
--Bryan

Bryan Olson

3/14/2008 8:25:00 AM

0

David S wrote:

> Using "C:\Program Files\apache-ant-1.7.0\bin\ant.bat" just gives me the same
> result.

Did you try the raw string, with the .bat extension? As in:

r'C:\Program Files\apache-ant-1.7.0\bin\ant.bat'


After Microsoft started allowing blanks in paths, it took them years to
fix many common cases where their own software choked on the paths. I
got into the habit of installing everything under c:\bin rather than
C:\Program Files. I still do that just to avoid writing essays into
my PATH variable.


--
--Bryan

David S

3/14/2008 11:39:00 AM

0

Hi,

Gets me further but still seems to be issue with space after 'Program' as
code tries to run 'C:\Program'. Don't understand what is going on here...

using "java.exe" from "C:\Program Files\Java\jdk1.6.0_01"
using "C:\Program Files\apache-ant-1.7.0\bin\ant.bat" for building
Building from E:\Red5\red5_server\install\windows
Cleaning old directories...
Compiling Java 1.5 version...
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "installer.py", line 132, in <module>
main()
File "installer.py", line 128, in main
builder.build()
File "installer.py", line 70, in build
self.compile(self.ant_cmd, os.path.join(red5_root, 'build.xml'), '1.5',
'cle
an', 'installerdist')
File "installer.py", line 26, in compile
assert os.system('%s -quiet -Djava.target_version=%s -buildfile %s%s' %
(ant
, version, script, args)) == 0
AssertionError


"Bryan Olson" <fakeaddress@nowhere.org> wrote in message
news:LxqCj.19741$Ch6.1841@newssvr11.news.prodigy.net...
> David S wrote:
>> I get
>>
>> ERROR: ""C:\Program Files\apache-ant-1.7.0\bin\ant"" does not exist
>>
>> If I cut the path statement here and paste it next to a windows XP
>> command prompt ant is invoked.
>>
>> The python code here is
>> if not os.path.isfile(ANT_CMD):
>> error('"%s" does not exist' % ANT_CMD)
>
> There you don't want the quotes within the string. On my MS-Win box:
>
> >>> import os
> >>> os.path.isfile(r'C:\Program Files\Windows NT\dialer.exe')
> True
> >>> print os.path.isfile(r'"C:\Program Files\Windows NT\dialer.exe"')
> False
>
>
> --
> --Bryan


Tim Golden

3/14/2008 12:04:00 PM

0

David S wrote:
> Gets me further but still seems to be issue with space after 'Program' as
> code tries to run 'C:\Program'. Don't understand what is going on here...

Slight apologies as I haven't followed this thread closely, but using
the Acrobat Reader executable, which is, I think, good enough for the
purposes of illustration:

<code>
import os
import subprocess

filename = r"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe"
doc = r"C:\Program Files\Adobe\Reader 8.0\Resource\ENUtxt.pdf"

print os.path.isfile (filename)

os.system (filename + " " + doc)

os.system ('"%s" "%s"' % (filename, doc))

subprocess.call ([filename, doc])

</code>

os.path.isfile succeeds
os.system (filename) fails as your code does
os.system ('"%s"' ...) fails even though both strings are requoted
subprocess.call (filename) succeeds

The latter, at least, is because the subprocess module
has some special-case handling for exactly this situation
on MS Windows, while os.system doesn't.

Now, ultimately, I don't know if this really helps your
exact situation but it least it should be clear what will
and what won't work. Conclusion: use subprocess.call if
you can.

TJG

David S

3/15/2008 12:16:00 AM

0

By mapping network drives in windows I can get past these issues with path
names.

Thanks,
David

"Tim Golden" <mail@timgolden.me.uk> wrote in message
news:mailman.1949.1205496184.9267.python-list@python.org...
> David S wrote:
>> Gets me further but still seems to be issue with space after 'Program' as
>> code tries to run 'C:\Program'. Don't understand what is going on here...
>
> Slight apologies as I haven't followed this thread closely, but using the
> Acrobat Reader executable, which is, I think, good enough for the
> purposes of illustration:
>
> <code>
> import os
> import subprocess
>
> filename = r"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe"
> doc = r"C:\Program Files\Adobe\Reader 8.0\Resource\ENUtxt.pdf"
>
> print os.path.isfile (filename)
>
> os.system (filename + " " + doc)
>
> os.system ('"%s" "%s"' % (filename, doc))
>
> subprocess.call ([filename, doc])
>
> </code>
>
> os.path.isfile succeeds
> os.system (filename) fails as your code does
> os.system ('"%s"' ...) fails even though both strings are requoted
> subprocess.call (filename) succeeds
>
> The latter, at least, is because the subprocess module
> has some special-case handling for exactly this situation
> on MS Windows, while os.system doesn't.
>
> Now, ultimately, I don't know if this really helps your
> exact situation but it least it should be clear what will
> and what won't work. Conclusion: use subprocess.call if
> you can.
>
> TJG


Josef Pktd

3/15/2008 1:06:00 AM

0

On Mar 14, 8:15 pm, "David S" <dav...@evertech.com.au> wrote:
> By mapping network drives in windows I can get past these issues with path
> names.
>
> Thanks,
> David
>
> "Tim Golden" <m...@timgolden.me.uk> wrote in message
>
> news:mailman.1949.1205496184.9267.python-list@python.org...
>
> > David S wrote:
> >> Gets me further but still seems to be issue with space after 'Program' as
> >> code tries to run 'C:\Program'. Don't understand what is going on here...
>
> > Slight apologies as I haven't followed this thread closely, but using the
> > Acrobat Reader executable, which is, I think, good enough for the
> > purposes of illustration:
>
> > <code>
> > import os
> > import subprocess
>
> > filename = r"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe"
> > doc = r"C:\Program Files\Adobe\Reader 8.0\Resource\ENUtxt.pdf"
>
> > print os.path.isfile (filename)
>
> > os.system (filename + " " + doc)
>
> > os.system ('"%s" "%s"' % (filename, doc))
>
> > subprocess.call ([filename, doc])
>
> > </code>
>
> > os.path.isfile succeeds
> > os.system (filename) fails as your code does
> > os.system ('"%s"' ...) fails even though both strings are requoted
> > subprocess.call (filename) succeeds
>
> > The latter, at least, is because the subprocess module
> > has some special-case handling for exactly this situation
> > on MS Windows, while os.system doesn't.
>
> > Now, ultimately, I don't know if this really helps your
> > exact situation but it least it should be clear what will
> > and what won't work. Conclusion: use subprocess.call if
> > you can.
>
> > TJG

I had the same problem recently with subprocess.popen, when there is a
space in the executable and a space in an additional argument as in
the acrobat example. I finally found a past thread in this news group
that explained that you have to use two (2) leading double quotes, and
as on the command line all arguments with spaces have to be quoted.

All three examples with acrobat work on my WindowsXP in this case
(opens Acrobat three times, after closing previous):

<code>
import os
import subprocess

filename = r"C:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe"
doc = r"C:\Program Files\Adobe\Acrobat 7.0\Help\ENU\Pdfmark.pdf"

print os.path.isfile (filename)
print 'case 1'
os.system ('""' + filename + '" "' + doc + '"') # quotes split up for
clarity: (' " " ' + filename + ' " " ' + doc + ' " ')
print 'case 2'
os.system ('""%s" "%s"' % (filename, doc))
print 'case 3'
subprocess.call ([filename, doc])

</code>

Josef