Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python操作系统模块_Python_Shell_Command Line - Fatal编程技术网

Python操作系统模块

Python操作系统模块,python,shell,command-line,Python,Shell,Command Line,我在Python的OS模块中尝试执行的是通常在命令行上执行的“注销”操作。我写道: os.system('logout') 但是,当我这样做时,我得到: sh: line 0: logout: not login shell: use `exit' 所以,我试着做到: os.system('exit') 它只从python程序中退出,不执行“注销”。 我该怎么做?是否可能?注销是一个shell命令(即bash或csh的一部分或您使用的任何命令)。它不是系统的可执行文件,只是脚本语言的一部分

我在Python的OS模块中尝试执行的是通常在命令行上执行的“注销”操作。我写道:

os.system('logout')
但是,当我这样做时,我得到:

sh: line 0: logout: not login shell: use `exit'
所以,我试着做到:

os.system('exit')
它只从python程序中退出,不执行“注销”。 我该怎么做?是否可能?

注销是一个shell命令(即bash或csh的一部分或您使用的任何命令)。它不是系统的可执行文件,只是脚本语言的一部分

问问自己你真正想要实现什么。

当一个以“登录shell”启动的shell退出时,就会发生“注销”的情况。没有其他程序可以执行
注销
命令,因为只有退出登录shell的操作才能执行注销


话虽如此,您可能能够找到属于当前用户的适当登录shell,并将其杀死。但是,请确保您选择的是正确的,用户可以多次登录,删除错误的用户会很烦人。

我刚刚看到您的问题,注意到您接受了一个不正确的答案

您要做的是启动shutdown cmd命令。这是一个非常强大的命令,需要您在相关PC上具有管理员访问权限

要注销计算机(但不强制退出),请在python中使用以下命令:

os.system("shutdown -l")
要关闭计算机并关闭所有程序,请使用以下命令:

os.system("shutdown -s -f")
os.system("shutdown -s -f -t <time> -c <message>
要关闭计算机,自动关闭所有程序并在设定的时间内显示自定义警告消息,请使用以下命令:

os.system("shutdown -s -f")
os.system("shutdown -s -f -t <time> -c <message>
os.system(“关机-s-f-t-c
在上面的示例中,替换为赢得用户的时间量(秒)。替换为在警告期间显示的自定义消息

希望这能对你有所帮助