Python文件在VSCode中工作,但作为可执行文件运行时会自动崩溃



入门

我正在开发一个程序,该程序可以计算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: ")

我找不到这个问题的直接解决方案,所以解决方法是通过在errors.txt文件中添加一行已知数据来消除函数getError()下对try...except ZeroDivisionError的需求。这样,可执行文件在尝试此尝试时不会崩溃,除非。

最新更新