we're talking about using the commands module to execute Linux shell commands, and when we write operations scripts in Python, we often need to execute the Linux shell commands, and the commands module in Python is dedicated to calling Linux Shell command, and return the status and results, here are the 3 main functions of the commands module:
1.commands.getoutput (' Shell command ')
2.commands.getstatus (' file ')
3.commands.getstatusoutput (' Shell command ')
Explain separately:
One, commands.getoutput (' Shell command ')
Execute shell command, return result (String type)
Import commandscommands.getoutput (' pwd ') #/users/admin/pycharmprojects/test
Second, commands.getstatus (' file ')
This function has been discarded by Python and is not recommended, it returns the result of the Ls-ld file (String) (It is too strange to return the result, no wonder it was discarded)
Import commandscommands.getstatus (' Admin.tar ') # '-rw-rw-r--1 Oracle 829440 Jan 10:36 Admin.tar '
Third, commands.getstatusoutput (' Shell command ')
Executes the shell command, returns a tuple of two elements tuple (status, result), status int, and result is of type string.
CMD is executed in {cmd;} 2>&1, so the returned result contains standard output and standard error. This is one of the most used functions.
Import commandscommands.getstatusoutput (' pwd ') # (0, '/users/admin/pycharmprojects/test ')
Python third-party library series of ten--commands Library