3

Trying to use xdg-open in a python script

xdg-open test.mp4

but get the following error:

File "dl.py", line 21
    xdg-open test.mp4
                ^
SyntaxError: invalid syntax

I can do it from command line so there is no issue with mp4

heroZero
  • 173
  • 3
  • 18

2 Answers2

3

I don't have enough reputation for a comment, but since no one else is replying I try to give an answer. I don't remember about 2.7 but in Python 3 I normally use something like this:

 os.system("xdg-open "+ <string>)

os.system is a command also in Python 2.7. You have to import os first. Hope it helps ;-)

archie
  • 109
  • 1
  • 6
2

This works on Linux, it will open the requested file with the appropriate software.

Use code below:

import subprocess
subprocess.call(["xdg-open", "filename"])
Til
  • 5,150
  • 13
  • 26
  • 34
Cat
  • 324
  • 2
  • 8
  • Fyi: An edit is not stealing your answer, you're still responsible and has credit for it. The edit usually is just to help improve it. And you can edit it again by the [edit](https://stackoverflow.com/posts/54253154/edit) button on the left side, between the `share` and `flag` button. – Til Jan 20 '19 at 08:16
  • the edit did not make sense and did not explain anything, thats all? – Cat Jan 21 '19 at 11:04
  • You can often see posts edited by another person. Most times are formating, and sometime it's to improve the posts' content. A better format is also important, apart from good content. – Til Jan 21 '19 at 11:09