-2

I know similar questions are apearing all the time, but I have read all I can find. Solutions there helped me a bit, but I still cannot build correct full path that contains whitespaces.

After research I found solution adding \ and take everyting in Single quote. So this path is acceptable, as you can see below.

import os
os.system('\"C:\Program Files (x86)\remar\remar.exe"')

But, the problem is that I have to add console commande to it, so the path have to look like that:

C:\Program Files (x86)\remar\remar.exe --break-in=null

I tried to concatenate several parts of the path, but result is unacceptable for os.system() expression.

I can't make something like that - "'\" + '"C:\Program Files' + ' ' + "(x86)\remar\remar.exe"'" + ' ' + '--break-in=null'

What shoud I do to get all parts together to one single correct path?

PS - I tried solutions from Windows path in Python , but it doesn't work for me. Maybe it's because python version. I'm using 3.61 BTW.

klakson
  • 3
  • 3
  • Possible duplicate of [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – tripleee Dec 12 '18 at 15:00
  • Try this : `command = "--break-in=null"` and then `os.system('\"C:\Program Files (x86)\remar\remar.exe" %s' %command)` – Sheldore Dec 12 '18 at 15:01
  • In addition to that, the spurious single quotes around the double-quoted string are wrong and superfluous. – tripleee Dec 12 '18 at 15:01
  • 3
    In addition to that, probably prefer `subprocess.run()` over `os.system()` – tripleee Dec 12 '18 at 15:02
  • @tripleee I tried that solutions earlier, but no luck. I will try `subprocess.run()` instead of `os.system()`. Maybe it could help. – klakson Dec 12 '18 at 15:09
  • @Bazingaa interesting. I will try that. Maybe it will work for me. Thank you! – klakson Dec 12 '18 at 15:09
  • This question is pretty much exactly what is dealt with here: https://stackoverflow.com/questions/204017/how-do-i-execute-a-program-from-python-os-system-fails-due-to-spaces-in-path – Karl Dec 12 '18 at 15:12
  • This is another reason why working with Windows is not fun. I guess you should make it a raw string and in there escape the whitespaces like so: `r"C:\Program\ Files\..."` – user8408080 Dec 12 '18 at 15:14
  • @user8408080 Thanks, but is it possible to mark string as raw on the fly? for example - `"r" + "C:\Program Files\..."` ? – klakson Dec 12 '18 at 15:16
  • what are you actually attempting to do? What command are you trying to run with os.system() ? I would assign the command to another variable, cmd, and then pass it as an argument. It looks like your trying to launch that executable .. i think subprocess.run() might be a better solution. https://docs.python.org/3/library/subprocess.html#module-subprocess – Oscalation Dec 12 '18 at 15:22
  • I think it's possible, when your string is for example `s1` , then you can convert it to a raw string with `s1_raw = "%r" %s1` if i'm not mistaken – user8408080 Dec 12 '18 at 15:25
  • @Oscalation I am trieng to join 3 parts of the path together to feed it in `os.system()` function. The one particular path containd the path of directory, the second - name of *.exe file, and the last one - console command that needs to be add in the end to run particular process. – klakson Dec 12 '18 at 15:27
  • @user8408080 thank you, I'll try that! – klakson Dec 12 '18 at 15:28
  • @klakson are you trying to splice together the full path because you are pulling them separately, or because you cant get it to work in one piece at once? – Oscalation Dec 12 '18 at 15:47
  • @Oscalation only because I'm pulling them separately – klakson Dec 12 '18 at 15:49
  • do you know the exact path? can you code in the path or do you need to discover where it is because it may change? If you need to discover, is the application in the env var PATH, If it is you can locate it via WHERE. If not you could maybe search for it with dir. – Oscalation Dec 12 '18 at 15:55
  • @Oscalation the full path needs to be generated on the fly. Some data are pulling from registry, some are hardcoded in dictionaries. Console commands are in dictionaries too. So, I'm getting values from dicts by the key (it's particular exe-file). – klakson Dec 12 '18 at 16:01
  • @klakson I would try building the path via os.path.join and use r'C:\Path to file' and another variable for the filename. This should fix your problem – Oscalation Dec 12 '18 at 16:15
  • Command escaping in Windows is nuts. There are so many weird special cases and places where the obvious assumption turns out to be wrong. It's best to avoid it altogether. I *believe* `subprocess.run` knows how to avoid all the weird issues, although other options that seem like they should avoid escaping issues (like the `os.exec*` family, which is the wrong tool anyway for other reasons) actually have escaping issues anyway. – user2357112 Dec 12 '18 at 16:45
  • @user2357112 Thank you. Totally agree BTW – klakson Dec 13 '18 at 22:00

1 Answers1

0

Try using os.path.join here. Don't end your path variable with a backslash, join will do that for you.

import os, subprocess
path = r"C:\Program Files (x86)\test"
filename = "test.exe"

result = os.path.join(path, filename)
print(result)
subprocess.run(result)

You may also find interest in pathlib Path

Oscalation
  • 370
  • 3
  • 15