AttributeError:“模块”对象没有属性“ startfile”

2 python python-2.7

尝试运行此程序,出现此错误:

Traceback (most recent call last):
  File "piltk.py", line 84, in <module>
    os.startfile(filename)
AttributeError: 'module' object has no attribute 'startfile'
Run Code Online (Sandbox Code Playgroud)

如何解决呢?

fel*_*xbr 5

在Linux上,您可以使用:

import subprocess, sys

opener ="open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, filename])
Run Code Online (Sandbox Code Playgroud)

这里采用

  • 如果不是很明显,这个答案也适用于 macOS(这就是“达尔文”的意思)。 (3认同)