导入os和time模块,写了一个自动关机(时间0.5)的代码,pyinstaller打包成exe文件,但是这个文件无法运行自动关机的内容
源码:
import os
import time
for i in range(10):
print(f'您还有{10-i}次机会')
li1=['0','1','2','3','4','5','6','7','8','9']
num=input('''请输入密码以解锁(密码为十以内的数字)''')
if num in li1:
num=int(num)
else:
print('请输入[0,9]内的数字')
if num==6:
current_os = os.name
if current_os == 'nt':
command = 'shutdown -s -t 0.5'
tip = '即将关机...'
elif current_os == 'pssix':
command = 'shutdown now'
else:
print('不支持的操作系统')
print(tip)
os.system(command)
break
elif num in range(0,5) or num in range(7,9):
print('密码错误')
打包:
在这个文件位置打开cmd,然后:
pyinstaller -F 自动关机代码.py
执行.py原文件的时候没有问题
执行dist文件夹里的exe文件时,其他功能都OK,但是运行到os.system(command)的时候cmd窗口闪了一下就没了