2

So I am wondering how I can execute an app via the python code but keep the code running?

Here is an example:

File1.py

from time import sleep
print("File1 has started")
sleep(3)
print("File1 has ended")

main.py

from time import sleep
print("main.py has started")
# start the File1.py here
sleep(2)
print("main.py is still running")
sleep(2)
print("main.py has ended")

I want the output to look like this:

>> main.py has started
>> File1 has started
>> main.py is still running
>> File1 has ended
>> main.py has ended

And is it possible to execute the files with the standard app that is assigned to the app? Example:

  • If someone has the default program for ".txt" Word, then word should open that file
  • If someone has the default program for ".txt" Notepad, then notepad should open that file.

Thanks!

  • 1
    Does this answer your question? [How to keep a Python script output window open?](https://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open) – Martynas Žiemys Jul 14 '23 at 13:41
  • 3
    I think you are supposed to ask one thing at a time and also put at least minimal effort to search for an already existing solution to your question. Seems like this is it for the second part: https://stackoverflow.com/questions/434597/open-document-with-default-os-application-in-python-both-in-windows-and-mac-os – Martynas Žiemys Jul 14 '23 at 13:43

1 Answers1

1

First of all, you need to organize the code in File1.py into a function, like this:

from time import sleep

def file_one_process():
    print("File1 has started")
    sleep(3)
    print("File1 has ended")

The you can call the function by importing the module at the top of main.py and calling the function, like this:

import File1

File1.file_one_process()

This produces the following output in the terminal:

File1 has started

(sleeps/pauses for 3 seconds, doesn't print anything)

File1 has ended

If you wanted to intertwine the prints of main.py and File1.py Al you have to do is create multiple functions for each of the prints in main.py and File1.py, then call them in the order you want.

If you aren't dealing with a Python file, (for example, a mov file) then you should look at this question. You can use this code to run a file called example.mov.

import subprocess, os, platform

if platform.system() == 'Darwin':       # macOS
    subprocess.call(('open', "example.mov"))

elif platform.system() == 'Windows':    # Windows
    os.startfile("example.mov")

else:                                   # linux variants
    subprocess.call(('xdg-open', "example.mov"))

This isn't my code, it's Nick's.

Ben the Coder
  • 539
  • 2
  • 5
  • 21
  • Yeah, but what if the file is a `.mov` file? And my standard program for these is DaVinci Resolve? Can I open the `.mov` file using the standard program but keep the code running? – Tousend1000 Jul 15 '23 at 08:12
  • 1
    @Tousend1000 Ah, I see. Your question showed a Python program, so I figured my answer should how to do that with Python. I will look to see if I can find anything on this on the internet. – Ben the Coder Jul 15 '23 at 13:23
  • @Tousend1000 Updated question! – Ben the Coder Jul 15 '23 at 13:31
  • 1
    Shouldn't you use `subprocess.Popen` so that opening the file does not block python's code? I believe that's closer to OP's intent. – Felix Fourcolor Jul 15 '23 at 13:55
  • I can't find any documentation on `subprocess.Popen`, are you sure it exists? – Ben the Coder Jul 15 '23 at 14:06
  • @BentheCoder See https://stackoverflow.com/a/39187984/7585848. TLDR: it's similar to `call`, except returns immediately and runs the command in background. – Felix Fourcolor Jul 15 '23 at 14:40