3

I am opening a pdf file with my default application using subprocess.call, like this:

subprocess.call(["xdg-open", pdf], stderr=STDOUT)

But, when running that, the process is attached to the terminal and I want to detach it. Basically, I want to run that and then be able to use the terminal for other stuff.

How would I go about doing that?

iamatrain
  • 131
  • 1
  • 6

1 Answers1

4

You can use Popen for this.

from subprocess import Popen, PIPE, STDOUT
p = Popen(["xdg-open", pdf], stderr=STDOUT, stdout=PIPE)
# do your own thing while xdg-open runs as a child process
output, _ = p.communicate()
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    Your code doesn't seem to do what I want. It does not detach from terminal. Doing stdout=subprocess.PIPE does detach from terminal, but it opens in the browser and not in the pdf-viewer. – iamatrain Jul 26 '18 at 12:58
  • 1
    Doing pdf = Popen(["xdg-open", pdf], stdin=None, stdout=None, stderr=None, close_fds=True, shell=False) fixed my problem. – iamatrain Jul 26 '18 at 13:03