1. Purpose
Every day at work, work needs, the computer needs to start some software every day, when off duty, need to turn off some software. One by one to open and close seems to be very cumbersome, so this script was generated.
2. Environment
System environment:
- win7-32 bit
- python 2.7.9
You also need to install pywin32.
pip install pywin32
3. Scripting
Starting the application script
#coding=utf-8 import win32api #Daily News Software Launch (0, 'open', r'C:\Program Files\Pudding\', '','',1) # OA activation (0, 'open', r'C:\Program Files\Tongda\ispirit\', '','',1) #QQ startup (0, 'open', r'D:\QQ\Bin\', '','',1) #...... # Of course you can add as much software as you need to launch
Terminate application scripts
#coding=utf-8 import os #Terminate QQ software ("taskkill /F /IM ") #Termination of the daily food ordering software ("taskkill /F /IM ") # Termination of OA software ("taskkill /F /IM ") #...... #Of course you can add as much software as you need to terminate
4. Production of exe
Finally, make an exe program file of these two scripts and put it on your desktop, so you can use it easily every time.
python invokes system commands to execute command lines
There are still more ways to call system commands in python, and there are ways to do it in the os/command/subprocess module
Compare it to other languages (and there aren't many that the owner has used):
PHP: exec(), system(), the overall feeling is not good, the blocking is very difficult to use
java: module is very powerful, nothing to say, the function is very similar to python's subprocess
(1)
Running system commands from a subterminal without access to the information returned after the command is executed
(2)
This method not only executes the command but also returns the executed message object
(3) Using the module commands module
There are two main methods in common use: getoutput and getstatusoutput
(4) Using the module subprocess
At the end of the day, it's subprocess that's the most powerful and can do a lot of things:
For example, recently encountered projects need to call shell commands in python, but also to get the call information, monitor the call process, timeout termination, etc., which requires that the call process does not block, but also interactive, found that the subprocess can fully meet the needs of the subprocess can be high!
Usage (file conversion example)
time_start = () cmd = "pdf2htmlEX --no-drm 1 --embed-css 0 --embed-image 0 --embed-font 0 --split-pages 1 --fit-width 748 --css-filename --dest-dir %s --embed-external-font 0 --auto-hint 1 %s" % (html_output_folder, src_file) cmd_list = (" ") sub2 = (cmd_list) i = 0 while 1: ret1 = (sub2) if ret1 == 0: time_end = () time_take = int(time_end - time_start + 0.5) with global_value_lock: success_ids[param[2]] = time_take print ,'end' break elif ret1 is None: print , 'running' if i >= max_check_time: time_end = () time_take = int(time_end - time_start + 0.5) with global_value_lock: timeout_ids[param[2]] = time_take () log_insert("%s%s%s" % (log_dir(output_folder), , "convert_log.txt"), src_file, "Timeout_Error", 'None') print "*****************Timeout_Error*****************" break (check_time) else: time_end = () time_take = int(time_end - time_start + 0.5) with global_value_lock: converterror_ids[param[2]] = time_take log_insert("%s%s%s" % (log_dir(output_folder), , "convert_log.txt"), src_file, "Process_Term_Error", str(ret1)) print ,'term', ret1, ret1 break i += 1
! Attention:When we use cmd directly instead of cmd_list, get the pid is not pdf2html process, but its parent process, remember to remember!
Here are some basic uses of Popen
Popen It has the following constructor:
(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
The parameter args can be a string or a sequence type (e.g., list, tuple) that specifies the process executable and its arguments. If it is a sequence, the first element is usually the path to the executable. You can also explicitly use the executeable parameter to specify the path to the executable. On Windows, Popen creates a child process by calling CreateProcess(). CreateProcess takes a string argument, and if the args are of a sequence type, the system will convert the sequence type to a string by using the list2cmdline() function.
Parameter bufsize: specifies the buffer. I'm still not sure what this parameter means, so I'm looking forward to the guidance of various experts.
The argument executable is used to specify an executable program. Normally we set the program to be run with the args parameter. If the shell parameter is set to True, executable specifies the shell to be used by the program; on Windows, the default shell is specified by the COMSPEC environment variable.
The parameters stdin, stdout, stderr indicate the standard input, output, and error handles of the program, respectively. They can be PIPEs, file descriptors, or file objects, or they can be set to None to indicate inheritance from the parent process.
The parameter preexec_fn, valid only on Unix platforms, is used to specify an executable object (callable object) that will be called before the child process is run.
Parameter close_sfs: on windows platform, if close_fds is set to True, the newly created child process will not inherit the input, output and error pipes of the parent process. We cannot set close_fds to True and redirect the standard input, output and error (stdin, stdout, stderr) of the child process at the same time.
If the argument shell is set to true, the program will be executed via shell.
The cwd parameter is used to set the current directory of the child process.
The parameter env is of dictionary type and is used to specify the environment variables for the child process. If env = None, the environment variables of the child process will be inherited from the parent process.
Arguments Universal_newlines:The line breaks for text are different under different operating systems. For example, windows uses '/r/n' for new, while Linux uses '/n'. If this parameter is set to True, Python uniformly treats these line breaks as '/n'.
The parameters startupinfo and createionflags are only available in windows, they will be passed to the underlying CreateProcess() function to set some properties of the child process, such as the appearance of the main window, the priority of the process, etc. The parameters startupinfo and createionflags are only available in windows, they will be passed to the underlying CreateProcess() function to set some properties of the child process.
When creating a Popen object, you can initialize the stdin, stdout or stderr parameters. Indicates the standard stream for communicating with the subprocess.
Used to initialize the stderr parameter when creating a Popen object, indicating that the error will be output via the standard output stream.
Popen's method:
()
Used to check if a child process has ended. Sets and returns the returncode property.
()
Wait for the child process to finish. Sets and returns the returncode property.
(input=None)
Interacts with child processes. Sends data to stdin or reads data from stdout and stderr. The optional argument input specifies the arguments to be sent to the child process. Communicate() returns a tuple: (stdoutdata, stderrdata). Note: If you wish to send data to a process via its stdin, the parameter stdin must be set to PIPE when the Popen object is created. likewise, if you wish to get data from stdout and stderr, you must set stdout and stderr to PIPE.
Popen.send_signal(signal)
Sends a signal to the child process.
()
Stop (stop) the child process. On windows platform, this method will call Windows API TerminateProcess() to end the child process.
()
Kill sub-processes.
If the stdin parameter was set to PIPE when the Popen object was created, a file object is returned for use in curating sub-processes to send instructions. Otherwise, None is returned.
If the stdout parameter was set to PIPE when the Popen object was created, a file object is returned for use by the child process to send instructions. Otherwise, None is returned.
If stdout was set to PIPE when the Popen object was created, a file object is returned for use by the child process to send instructions. Otherwise, None is returned.
Get the process ID of the child process.
Gets the return value of the process. Returns None if the process has not ended.
This above python start application and terminate the application is all that I have shared with you, I hope to give you a reference, and I hope that you will support me more.