Open and Close Computer Application in Python

In the following programs you will learn that how to open and close computer application. And how to print text file text on Printer.

Program Opening Computer File and Closing

Program

import subprocess
notepadProcess = subprocess.Popen("Notepad")
while notepadProcess.poll() is None:
    print('still running')
    reply = input("Kill process ? (type yes) ")
    print('>>|'+reply.strip()+'|<<')
    if reply.strip()=='yes':
        print("closing process")
        notepadProcess.terminate()
Output
still running
Kill process ? (type yes) yes
>>|yes|<<
closing process
still running
Kill process ? (type yes) yes
>>|yes|<<
closing process

Program Closing Computer Opened File

Program

import os
os.system("TASKKILL /F /IM notepad.exe")
Output
SUCCESS: The process "notepad.exe" with PID 8472 has been terminated.

Computer Shut Downing

Program

import os
print("Start process to shut down computer")
os.system("Shutdown -s") #Shutdowning Computer.
Output
Start process to shut down computer
...

Printing Text of Text File on Printer
Program

import os
print("Start process to print text of MyTxtFile text file")
os.startfile("C://MyTxtFile.txt", "print")
Output
Start process to print text of MyTxtFile text file
...