Search code examples
pythonwindowscommandexternal

Python script waits external command to be finished before continuing


I have a very simple Python script which parse somme data and write the output in a text file.

with open(debug, 'w') as debugFile:
 debugFile.write(metreDebug)

At the end of the script I want to open this text file so that the user can directly see the output.

osCommandString = "notepad.exe " + debug
os.system(osCommandString)

Yes... this is a developpement on windows. Unfortunately, the python script waits that user close the notepad before continuing.

Any ideas on how to fix that issue?

Thanks for the help.


Solution

  • Use Subprocess module instead.

    import subprocess
    with open(debug, 'w') as debugFile:
     debugFile.write(metreDebug)
    
    osCommandString = "notepad.exe " + debug
    subprocess.Popen(osCommandString)
    

    You can read more about it here:

    https://docs.python.org/2/library/subprocess.html