Browse Source

完善部分异常的处理,增加程序健壮性

剑断了 2 years ago
parent
commit
f2092d283d
2 changed files with 32 additions and 6 deletions
  1. 19 4
      FileOperator.py
  2. 13 2
      main.py

+ 19 - 4
FileOperator.py

@@ -257,8 +257,13 @@ def SaveForOutput(path, fileName):
     # fileName = "localPath.config"
     # fileName = "localPath.config"
     fullpath = os.path.join(path, fileName)
     fullpath = os.path.join(path, fileName)
     if not os.path.exists(fullpath):  # 如果路径不存在,创建路径,写入文件,返回False
     if not os.path.exists(fullpath):  # 如果路径不存在,创建路径,写入文件,返回False
-        file = open(fileName, "w", encoding="utf-8")
-        file.close()
+        try:
+            file = open(fileName, "w", encoding="utf-8")
+        except:
+            return
+        finally:
+            if file:
+                file.close()
         return False
         return False
     else:
     else:
         return True
         return True
@@ -276,6 +281,16 @@ def ReadForOutput(path):
 
 
 # 写入首行的内容
 # 写入首行的内容
 def WriteForOutput(path, downloadPath, outputPath):
 def WriteForOutput(path, downloadPath, outputPath):
-    with open(path, "w", encoding="utf-8") as f:
-        f.write(downloadPath+'\n')
+    try:
+        f = open(path, "w", encoding="utf-8")
+        f.write(downloadPath + '\n')
         f.write(outputPath)
         f.write(outputPath)
+    except:
+        return
+    finally:
+        if f:
+            f.close()
+    # with open(path, "w", encoding="utf-8") as f:
+    #     f.write(downloadPath+'\n')
+    #     f.write(outputPath)
+

+ 13 - 2
main.py

@@ -172,7 +172,12 @@ class MainApp(QMainWindow, Ui_MainWindow):
     # 设置UI
     # 设置UI
     def SetUI(self):
     def SetUI(self):
         tmp = open('tmp.png', "wb+")
         tmp = open('tmp.png', "wb+")
-        tmp.write(base64.b64decode(img))
+        try:
+            tmp.write(base64.b64decode(img))
+        except Exception as e:
+            QMessageBox.critical(self, "错误", "请关闭本程序并使用管理员权限运行本软件!或卸载本软件再重新安装至其它非系统盘 比如:D盘、E盘!" + str(e))
+            return
+
         tmp.close()
         tmp.close()
         icon = QIcon('tmp.png')
         icon = QIcon('tmp.png')
         os.remove("tmp.png")
         os.remove("tmp.png")
@@ -228,6 +233,7 @@ class MainApp(QMainWindow, Ui_MainWindow):
                     outputPath = FileOperator.MakeDir(outputPath, dviInfoList[3])
                     outputPath = FileOperator.MakeDir(outputPath, dviInfoList[3])
                 except Exception as e:
                 except Exception as e:
                     QMessageBox.critical(self, "错误", "已经存在同名文件夹! Error:" + str(e))
                     QMessageBox.critical(self, "错误", "已经存在同名文件夹! Error:" + str(e))
+                    return
 
 
                 if self.isSpiderMode:
                 if self.isSpiderMode:
                     self.Log("开始爬取BV:{0}, 标题:{1} 的所有视频标题,请稍后...".format(dviInfoList[1], dviInfoList[3]))
                     self.Log("开始爬取BV:{0}, 标题:{1} 的所有视频标题,请稍后...".format(dviInfoList[1], dviInfoList[3]))
@@ -235,6 +241,7 @@ class MainApp(QMainWindow, Ui_MainWindow):
                         TitleSpider.GetTxt(dviInfoList[1], outputPath)
                         TitleSpider.GetTxt(dviInfoList[1], outputPath)
                     except Exception as e:
                     except Exception as e:
                         QMessageBox.critical(self, "错误", "请检查网络后重试 Error:" + str(e))
                         QMessageBox.critical(self, "错误", "请检查网络后重试 Error:" + str(e))
+                        return
                     # 调用爬虫产生.txt
                     # 调用爬虫产生.txt
                     global fileName
                     global fileName
                     fileName = TitleSpider.fileName
                     fileName = TitleSpider.fileName
@@ -253,7 +260,11 @@ class MainApp(QMainWindow, Ui_MainWindow):
                 mp4List = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[0]  # mp4真正在的地方
                 mp4List = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[0]  # mp4真正在的地方
                 # Log
                 # Log
                 mp4nameList = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[1]
                 mp4nameList = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[1]
-                mp4nameList.sort(key=GetSeries)
+                try:
+                    mp4nameList.sort(key=GetSeries)
+                except Exception as e:
+                    QMessageBox.critical(self, "错误", "存在干扰文件!排序错误,请联系作者!" + str(e))
+                    return
                 s = "查询到以下mp4文件:\n"
                 s = "查询到以下mp4文件:\n"
                 for item in mp4nameList:
                 for item in mp4nameList:
                     s += (item + '\n')
                     s += (item + '\n')