Operating cmd python
We can usually use the os module command execution cmd
Method One: os.system
os.system(执行的命令) # 源码 def system(*args, **kwargs): # real signature unknown """ Execute the command in a subshell. """ pass
Method two: os.popen (command execution)
os.popen (command execution) # 源码 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)
Difference between the two
- Only the content of the system can return back to the input, wherein the code 0 indicating success. But we have no way to get information content output
- popen can acquire the content output information , it is an object, by () .read to read