Launching command on windows

A

Alexandre Badez

Hy,

I'm working on windows and I try to do something like:

import os
APP = os.path.abspath("C:\\Program Files\\Notepad++\\notepad++.exe")
FILE1 = os.path.abspath("D:\\Documents and settings\\test1.py")
FILE2 = os.path.abspath("D:\\Documents and settings\\test2.py")
command = '"%(app)s" "%(file1)s" "%(file2)s"' % {
'app' : APP,
'file1' : FILE1,
'file2' : FILE2}
# === FOR 'DEBUG' ===
print APP
print FILE1
print FILE2
print command
print repr(command)
# === END FOR 'DEBUG' ===
os.system(command)


This code give in output:
C:\Program Files\Notepad++\notepad++.exe
D:\Documents and settings\test1.py
D:\Documents and settings\test2.py
"C:\Program Files\Notepad++\notepad++.exe" "D:\Documents and settings
\test1.py" "D:\Documents and settings\test2.py"
'"C:\\Program Files\\Notepad++\\notepad++.exe" "D:\\Documents and
settings\\test1.py" "D:\\Documents and settings\\test2.py"'

'C:\Program' n'est pas reconnu en tant que commande interne
ou externe, un programme ex,cutable ou un fichier de commandes.
# <= My windows is a french one
# This error message could be translated as:
# 'c:\Program' is not an internal nor external command, an executable
program nor a command file

But if I copy the command in the output, an paste it in a console, it
work very well.
Does any of you know what I can do ?

PS: I think I'm oblige to add " neer every path for spaces in path,
but if you know an other way, it could be cool :)
 
M

mauro

Hy,

I'm working on windows and I try to do something like:

import os
APP = os.path.abspath("C:\\Program Files\\Notepad++\\notepad++.exe")
FILE1 = os.path.abspath("D:\\Documents and settings\\test1.py")
FILE2 = os.path.abspath("D:\\Documents and settings\\test2.py")
command = '"%(app)s" "%(file1)s" "%(file2)s"' % {
'app' : APP,
'file1' : FILE1,
'file2' : FILE2}
# === FOR 'DEBUG' ===
print APP
print FILE1
print FILE2
print command
print repr(command)
# === END FOR 'DEBUG' ===
os.system(command)

This code give in output:
C:\Program Files\Notepad++\notepad++.exe
D:\Documents and settings\test1.py
D:\Documents and settings\test2.py
"C:\Program Files\Notepad++\notepad++.exe" "D:\Documents and settings
\test1.py" "D:\Documents and settings\test2.py"
'"C:\\Program Files\\Notepad++\\notepad++.exe" "D:\\Documents and
settings\\test1.py" "D:\\Documents and settings\\test2.py"'

'C:\Program' n'est pas reconnu en tant que commande interne
ou externe, un programme ex,cutable ou un fichier de commandes.
# <= My windows is a french one
# This error message could be translated as:
# 'c:\Program' is not an internal nor external command, an executable
program nor a command file

But if I copy the command in the output, an paste it in a console, it
work very well.
Does any of you know what I can do ?

PS: I think I'm oblige to add " neer every path for spaces in path,
but if you know an other way, it could be cool :)

If you don't mind using spawnl instead of system, this should work
even with spaces:
os.spawnl(os.P_NOWAITO, command)
I hope it helps.

Mauro
 
K

kyosohma

Hy,

I'm working on windows and I try to do something like:

import os
APP = os.path.abspath("C:\\Program Files\\Notepad++\\notepad++.exe")
FILE1 = os.path.abspath("D:\\Documents and settings\\test1.py")
FILE2 = os.path.abspath("D:\\Documents and settings\\test2.py")
command = '"%(app)s" "%(file1)s" "%(file2)s"' % {
'app' : APP,
'file1' : FILE1,
'file2' : FILE2}
# === FOR 'DEBUG' ===
print APP
print FILE1
print FILE2
print command
print repr(command)
# === END FOR 'DEBUG' ===
os.system(command)

This code give in output:
C:\Program Files\Notepad++\notepad++.exe
D:\Documents and settings\test1.py
D:\Documents and settings\test2.py
"C:\Program Files\Notepad++\notepad++.exe" "D:\Documents and settings
\test1.py" "D:\Documents and settings\test2.py"
'"C:\\Program Files\\Notepad++\\notepad++.exe" "D:\\Documents and
settings\\test1.py" "D:\\Documents and settings\\test2.py"'

'C:\Program' n'est pas reconnu en tant que commande interne
ou externe, un programme ex,cutable ou un fichier de commandes.
# <= My windows is a french one
# This error message could be translated as:
# 'c:\Program' is not an internal nor external command, an executable
program nor a command file

But if I copy the command in the output, an paste it in a console, it
work very well.
Does any of you know what I can do ?

PS: I think I'm oblige to add " neer every path for spaces in path,
but if you know an other way, it could be cool :)



I got it to work using subprocess.Popen

Not sure why it doesn't work with os.system though.

Mike
 
A

Alexandre Badez

I got it to work using subprocess.Popen

Not sure why it doesn't work with os.system though.

Mike

Thanks Mike and Mauro,

Mauro, your solution do not seems to work (or I made a mistake..)
Mike your solution work great, thanks.
But, I steel think it's a bug (python or window ??).
I will try to take a look about it, when I have time.

Alex
 
K

kyosohma

Thanks Mike and Mauro,

Mauro, your solution do not seems to work (or I made a mistake..)
Mike your solution work great, thanks.
But, I steel think it's a bug (python or window ??).
I will try to take a look about it, when I have time.

Alex

Some of those spawn methods from the os module don't work on Windows
although that one doesn't seem to be one of the excluded:
http://docs.python.org/lib/os-process.html.

You should use subprocess regardless as the os.system and os.spawn*
calls are to be deprecated.

http://docs.python.org/lib/module-subprocess.html

Replacing os.Popen* and os.spawn* calls with subprocess:
http://docs.python.org/lib/node534.html
http://docs.python.org/lib/node537.html

Mike
 

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

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top