Search code examples
pythonpython-3.xcmdwindowpython-3.4

Python "call()" function doesn't accept path to file from "abspath()"


Probably this question is obvious, so excuse me.

I want to execute shell command (Windows 8.1, Python 3.4) to open IE with SVG file. I do it like this:

# imgpath = 'C:/Users/Vladimir/dot-code\\..\\graph1.svg'
tmp = FS.abspath(imgpath)
# tmp = 'C:\\Users\\Vladimir\\graph1.svg'
subprocess.call(["start", "", tmp])

Looks fine, but I get an exception inside call() - FileNotFoundError: [WinError 2] File not found.

I suppose the root of evil is double slashes in "tmp". How can I fix it?


Solution

  • You should not pass the empty string. (I guess, you meant to separate the command and argument.). Remove blank string. Just pass start and the path.

    In addition to that start is not a real program, but a command builtin to cmd. Use cmd /c:

    subprocess.call(['cmd', '/c', 'start', tmp])
    

    or pass shell=True keyword argument:

    subprocess.call(['start', tmp], shell=True)
    

    BTW, on windows, you can use os.startfile:

    import os
    os.startfile(tmp)