1

I am making a program that is making .docx files for the user, and i want my script to open the file when it is done. I then want the script to end, but when the script ends the .docx file closes immediately.

To open the file i use: proc=os.popen(fileName)

Any help would be great.

I am using python 3.10 and windows 10.

  • Does this help* https://stackoverflow.com/questions/19447603/how-to-kill-a-python-child-process-created-with-subprocess-check-output-when-t – MSH Dec 22 '21 at 22:49
  • `popen()` is exactly the _wrong_ thing to use for this: It sets up a FIFO between your parent process and the child's stdout. In doing so, it makes the child more dependent on the parent than it would be if you used almost anything else. – Charles Duffy Dec 22 '21 at 23:05
  • thanks @Charles, what would u suggest i use? – Øystein Bringsli Dec 22 '21 at 23:26

1 Answers1

0

You may use subprocess module. I found a similar question to your and I do encourage reading all answers and specially @jfs answer

https://stackoverflow.com/a/15055133/5203248

I will quote the important part from his answer to how to do it with subprocess

If you want to run a specific application then you could use subprocess module e.g., Popen() allows to start a program without waiting for it to complete:

import subprocess

p = subprocess.Popen(["notepad.exe", fileName])
# ... do other things while notepad is running
returncode = p.wait() # wait for notepad to exit

There are many ways to use the subprocess module to run programs e.g., subprocess.check_call(command) blocks until the command finishes and raises an exception if the command finishes with a nonzero exit code.