Python: Use the os module to execute cmd commands

Hello everyone, my name is wangzirui32, today we will learn how to make Python execute cmd commands.
Very simple, enter the code:

from os import system
# 在system里放入需要执行的命令即可 这里我写的是ipconfig命令
ok = system("ipconfig")
# 函数执行后如果没有错误则返回0 否则返回1
print(ok)

Run the code to execute the cmd command.
If you want to get the content returned after executing the command, you can use the popen function:

from os import popen
# 在popen里放入需要执行的命令即可 这里我写的是ipconfig命令
text = popen("ipconfig").read()
print(text)

That's it, have you learned it?

Guess you like

Origin blog.csdn.net/wangzirui32/article/details/114789046