初学者在这里
我正在开发一个程序,该程序可以计算3D打印机(通过Cura Slicer)打印所需的估计时间与实际时间之间的累积误差。我想使用pyinstaller将程序作为可执行文件运行,但是命令提示符下打开的可执行文件会自动关闭。我可以使用input("Press any key to stop the program: ")
运行可执行文件并关闭窗口,但是在实现try: error_list=[]...except ZeroDivisionError: print("Sorry, there's nothing in this file").
之后,终端窗口将在运行时立即关闭。请记住,我在VSCode中测试了代码,input()
可以按预期工作,但是在终端中作为可执行文件会自动关闭。
为什么程序在VSCode中可以正常工作,但是以可执行文件身份运行时会自动关闭?非常感谢您的帮助!
要在cmd中安装可执行文件,请使用pyinstaller .\CuraTimeError.py
def getError():
f=open("errors.txt","r")
data=f.read()
try:
error_list=[]
interations=iter(data)
for char in range(len(data)):
try:
thing=next(interations)
except(StopIteration):
continue
if thing == "*":
error=float(next(interations)+next(interations)+next(interations)+next(interations))
error_list.append(error)
print("The current total error is: " +str(sum(error_list)/len(error_list)))
f.close()
except ZeroDivisionError:
print("Sorry, there's nothing in this file")
getError()
numberOfCases=int(input("How many tests are you inputting? "))
if numberOfCases != 0:
print("NOTE: Time follows 24-hour notation")
for i in range(numberOfCases):
print("Please enter case number "+str(i+1))
f=open("errors.txt","a")
curaTime=input("Cura estimated time: ")
startTime=input("Start time: ")
endTime=input("End time: ")
if int(endTime)<int(startTime):
endTime = str(int(endTime)+2400)
actualTime=60*(int(endTime[0:2])-int(startTime[0:2]))+(int(endTime[2:4])-int(startTime[2:4]))
estimatedTime= 60*(int(curaTime[0:2]))+int(curaTime[2:4])
error=(actualTime-estimatedTime)/(estimatedTime)+1
f.write(curaTime+","+startTime+","+endTime+",*"+str(round(error,2))+"\n")
f.close()
getError()
input("Press any key to stop the program: ")
我无法找到直接解决此问题的方法,因此解决方法是通过在try...except ZeroDivisionError
文件中添加一行已知数据来消除对函数getError()
下对errors.txt
的需要。这样,尝试执行此try-except时可执行文件不会崩溃。