It is true that the os.system will run the binary with spaces in the path, enclosing the path in quotation marks. (This should be a fairly obvious solution if you are used to using the terminal.) However, this alone does not solve the more painful problem with this function ... Once you do this, you may run into problems by adding arguments to your command ! (Ahh!)
All current recommendations now use the subprocess module instead of this old, frowning function. You can also use shlx to convert flat strings to lists for these subprocess functions. I also encountered problems or difficulties with these methods, which I won’t talk about ... Also, sometimes it is just easier to use os.system when all you need is a thin shell over a shell that implicitly displays output streams. on the console, works synchronously, etc. I would really like to have a built-in function to execute such a command on the shell, with absolutely zero parsing, wrapping, abstraction ...
Since there is no built-in without "filters", here is my solution patch for os.system . This is taken from my open source library. This has been tested on Windows, Mac, and Ubuntu Linux. I know that it’s not 100% reliable, and it’s more connected with a tan that you could hope for, but it’s not so bad.
When you call this _system() (passing the string to execute), simply _system() your long path in quotation marks and include any arguments you need with and without quotation marks. On the first “token” in the command, this will remove quotes and spaces in the path on Mac or Linux. On Windows, it uses the "short name", effectively deciding what it is in the given environment. This part of the code is a bit more complicated. It mainly uses a batch mechanism to resolve names and sends the results back through stderr in order to analyze what you would otherwise get for Popen() results on stdout. Here you can also use the working directory option and set it as an alternative solution.
I think I included all imports and determined what you need. If I missed something (copying and pasting the source spins), let me know.
from os import system, getcwd, chdir from subprocess import Popen, PIPE import platform __plat = platform.system() IS_WINDOWS = __plat == "Windows" IS_LINUX = __plat == "Linux" IS_MACOS = __plat == "Darwin" __SCRUB_CMD_TMPL = "{0}{1}" __DBL_QUOTE = '"' __SPACE = ' ' __ESC_SPACE = '\\ ' if IS_WINDOWS : __BATCH_RUN_AND_RETURN_CMD = ["cmd","/K"]
Buvinj
source share