0

How can I run a command that requires a filepath that contains spaces when using the start command with os.system

For Example:

# path_d[key] = C:\Users\John\Documents\Some File With Space.exe
path = path_d[key]
os.system("start {0}".format(path))

When I try running it I end up getting an error saying:

Windows cannot find 'C:\Users\John\Documents\Some.'. Make sure you typed the name correctly, and then try again.
tyleax
  • 1,556
  • 2
  • 17
  • 45
  • Possible duplicate of [os.system to invoke an exe which lies in a dir whose name contains whitespace](https://stackoverflow.com/questions/6977215/os-system-to-invoke-an-exe-which-lies-in-a-dir-whose-name-contains-whitespace) – rawwar Jun 14 '18 at 09:13

2 Answers2

1

You need to properly escape your special characters in path, which might be easily done as such:

path = r"C:\Users\John\Documents\Some File With Space.exe"

To execute it under Windows:

import os

os.system(r"C:\Users\John\Documents\Some File With Space.exe")

EDIT

per request of OP:

path_dict = {"path1": r"C:\Users\John\Documents\Some File With Space.exe"}
os.system('{}'.format(path_dict["path1"]))
Fourier
  • 2,795
  • 3
  • 25
  • 39
  • 1
    How could I make it if the filepath is stored as a dictionary and not hardcoded? – tyleax Jun 14 '18 at 09:00
  • 1
    @InAFlash I added (edited) the post including the way I execute it and it works fine under python2 on my machine – Fourier Jun 14 '18 at 09:07
1

i do the following

path = path_d[key]
os.system(r'start "{0}"'.format(path))

so, surround the path with double quotes. that way it will take care of spaces in path. if there is no default application to open, it might open command prompt. So, if its a text file do the following

os.system(r'notepad "{0}"'.format(path))
rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 1
    I tried this and it seemed to result in opening a command prompt with the title as the path name – tyleax Jun 14 '18 at 09:04
  • 1
    if its an exe, you directly give path to the exe instead of putting start infornt of it – rawwar Jun 14 '18 at 09:05
  • 2
    I found that `os.system(r'start "random title" "{0}"'.format(path))` worked well. MS-DOS 'start' command expects a title for the first argument if you use quotations. – tyleax Jun 14 '18 at 09:08
  • @Sam, looks like its already answered at https://stackoverflow.com/questions/6977215/os-system-to-invoke-an-exe-which-lies-in-a-dir-whose-name-contains-whitespace – rawwar Jun 14 '18 at 09:14
  • It seems the question you linked doesn't include the use of "start" call. I've edited my question to reflect that. – tyleax Jun 14 '18 at 17:05
  • Did you atleast try the same command from command line and see what's happening.because..when I used start with a text file it did open a new command prompt – rawwar Jun 14 '18 at 17:07
  • When I use the start command you provided it would open a command prompt and did not start the file with the default application. When I use the command i specified in comment above starts the file. The command you provided opens a command prompt with the title as the path specified. I was able to see arguments of start command here: https://www.computerhope.com/starthlp.htm – tyleax Jun 14 '18 at 17:17
  • Just wondering..usually it's better to use subprocess modules popen function. Do you want to try that? – rawwar Jun 14 '18 at 17:19