We are doing automated phone side, when sometimes you may encounter need to perform cmd command complete use case, our study together, through the python script execution cmd command
What is cmd
cmd is the command abbreviation. That command prompt (CMD), is in OS / 2, Win-based operating systems (including Windows 2000 and XP, Vista in, and Server 2003) "MS-DOS mode" under. Chinese version of Windows XP in command prompt to further improve compatibility with the DOS operating commands, the user can enter the Chinese call the file directly from the command prompt ---- Baidu Encyclopedia
In fact, it means to execute commands in place of windows.
Operating cmd python
We can usually use the os module command execution cmd
method 1:
os.stsrem (command execution) # source code DEF System (* args, ** kwargs): # Real Signature Unknown "" " the Execute the Command in at The A subshell. " "" Pass
We can see os.system the success of the content of our input to return back where the code of 0 indicates successful execution. But we have no way to get information content output
Method 2:
os.popen(执行的命令) # 源码 def popen(cmd, mode="r", buffering=-1): if not isinstance(cmd, str): raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) if mode not in ("r", "w"): raise ValueError("invalid mode %r" % mode) if buffering == 0 or buffering is None: raise ValueError("popen() does not support unbuffered streams") import subprocess, io if mode == "r": proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, bufsize=buffering) return _wrap_close(io.TextIOWrapper(proc.stdout), proc) else: proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, bufsize=buffering) return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
Can be found in the contents of the output is an object, the default is read Open
We can read the file object, get information returned content
We can perform here cmd command returns the content acquisition, to return the contents were taken out to facilitate our other tests. Remove the return information reference appium --- Android app resource monitoring
If you write for your help, a point of concern. Continuously updated in ~