【问题标题】:Using os.system() to shutdown a computer and use pyinstaller使用 os.system() 关闭计算机并使用 pyinstaller
【发布时间】:2020-09-16 21:00:00
【问题描述】:
我在 python 3.8 中编写了以下代码
import os
log=open('userlog.txt', 'a')
log.write(os.getlogin())
log.write('\n')
log.close()
os.system('shutdown /s /t 0')
当我正常运行它时,它可以正常运行:它将用户名添加到文本文件中,然后关闭计算机。但是,当我使用 pyinstaller 将其转换为 exe 时,它不起作用。相反,它会打开空白的命令提示符窗口,无法每秒输入一次。有什么原因会发生这种情况?
我使用的是 Windows 10。
【问题讨论】:
标签:
python
operating-system
pyinstaller
python-3.8
【解决方案1】:
在 Windows 10 上使用 Python 3.6 和 PyInstaller 3.6 编译脚本对我有用。你可能编译不正确。对我来说,以下步骤在命令提示符中有效(注意 %USERNAME% 是您的用户名)。
cd "C:\Users\%USERNAME%\path\to\script\"
pyinstaller script.py
第一步涉及将当前工作目录更改为包含脚本的目录。第二步只是常规的 PyInstaller 编译过程。
如果这不起作用,请检查 PyInstaller 编译的输出和/或 PyInstaller 的任何错误日志。这可能会帮助您找到问题所在。