9

Trying to run this program, I got this error:

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

How to fix this ?

3 Answers3

19

On Linux you can use:

import subprocess, sys

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

Adopted from here

felixbr
  • 843
  • 1
  • 7
  • 18
1

Given that you are not running on Windows you cannot use os.startfile. If you want to launch another process you could use os.system or look at the subprocess module

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • 3 years later, still nothing that properly documents this, while still talking about using windows paths in the [same doc](https://docs.python.org/3.6/library/os.html#os.startfile). – not2qubit Nov 03 '18 at 01:21
  • @not2qubit if you look at the bottom of that description you link to you can see it says "Availability: Windows" which is how it documents for each function which platforms you can expect it to work on (contrast with os.system right below it that has "Availability: Unix, Windows") – Eric Renouf Nov 03 '18 at 01:27
  • That is what I was saying. It claim to work on Windows, but doesn't, at least not for me on Python3.6. – not2qubit Nov 03 '18 at 01:52
  • @not2qubit you might need to start a new question then, I just downloaded the latest 3.7.1 for Windows and it worked fine to open a word document with word for me – Eric Renouf Nov 03 '18 at 13:15
  • Hm, ok, perhaps I'm not using it right then. Can you provide a usage example? – not2qubit Nov 03 '18 at 16:31
  • @not2qubit I just did `os.startfile(r'c:\Users\erenouf\Documents\resume.odt')` and it launched Word to edit that file – Eric Renouf Nov 03 '18 at 18:16
1

Maybe so:
os.system('xdg-open аny_file')

Mr.Леон
  • 144
  • 1
  • 6