Browse Source

v3.0 * 不再需要手动创建输出文件夹了
* 修复了获取子文件夹的.info文件导致的infolist返回异常的bug
* 修复了输出目录存在其他文件或文件夹时导致的闪退的bug
* 修复了只下载了部分视频导致命名错乱和越界异常的bug

剑断了 2 years ago
parent
commit
979b3efa8b
1 changed files with 45 additions and 26 deletions
  1. 45 26
      FileOperator.py

+ 45 - 26
FileOperator.py

@@ -4,6 +4,7 @@
 # @File : FileOperator.py
 # @Software : PyCharm
 import os
+import random
 import re
 import shutil
 
@@ -81,47 +82,55 @@ def MoveFile(srcFileList, dstFolder):
 
 # 排序用到的key
 def GetSeries(dataList):
-    return int(dataList.split('_')[1])
+    return int(dataList.split('_')[-2])
 
 
-def DoRename(path, fileName):
+def DoRename(path, fileName, aID):
     # 获取.txt文件名
     filName = fileName
     # 读取.txt文件
     with open(filName, encoding='UTF-8') as f:
         lines = f.readlines()  # 新文件名按行保存
-    fileList = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
-    fileList.sort(key=GetSeries)
-    i = 0
-    for files in fileList:  # 遍历所有文件
-        oldDir = os.path.join(path, files)  # 原来的文件路径
-        if os.path.isdir(oldDir):  # 如果是文件夹则跳过
-            continue
-        filetype = os.path.splitext(files)[1]  # 文件扩展名
-        newDir = os.path.join(path, str(i+1) + '. ' + lines[i].strip('\n') + filetype)  # 新的文件路径
-        os.rename(oldDir, newDir)  # 重命名
-        i = i + 1
 
+    # 提取出含有指定特征的fileList
+    fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
+    fileList = []  # 存储要copy的文件全名
+    # 获取要被命名的文件,包含了文件夹有其它文件或文件夹的情况
+
+    for dirPath, dirNames, fileNames in os.walk(path):
+        for file in fileNames:
+            if aID in file and file.split('.')[-1] in fileTypeList:  #
+                oldName = os.path.join(dirPath, file)  # 文件全名
+                if os.path.isdir(oldName):
+                    continue
+                fileList.append(oldName)
+
+    fileList.sort(key=GetSeries)
+    # fileList = set(fileList)  # 防止文件重复
 
+    for oldDir in fileList:
+        filetype = '.' + oldDir.split('.')[-1]
+        index = int(oldDir.split('_')[-2]) - 1
+        newDir = os.path.join(path, str(index + 1) + '. ' + lines[index].strip('\n') + filetype)  # 新的文件路径
+        os.rename(oldDir, newDir)  # 重命名
 
 
-def GetInfoList(path):
-    fileTypeList = ['info']
+def GetInfoList(path, aID):
+    fileTypeList = aID + '.info'
     fileList = []  # 含路径的文件名
 
     for dirPath, dirNames, fileNames in os.walk(path):
         for file in fileNames:
-            fileType = file.split('.')[-1]
-            if fileType in fileTypeList:
+            if file == fileTypeList:
                 file_fullname = os.path.join(dirPath, file)  # 文件名
                 fileList.append(file_fullname)
     fileList.sort(key=GetFileSeries)  # 这里必须排序
     return fileList
 
 
-def GetLocalVideoTitle(path):
-    fileList = GetInfoList(path)
-    print(fileList)
+def GetLocalVideoTitle(path, aID):
+    fileList = GetInfoList(path, aID)
+    #  print(fileList)
     titleList = []
     findVideoTitle = re.compile(r'"PartName":"(.*?)"')
     for infoFile in fileList:
@@ -133,9 +142,9 @@ def GetLocalVideoTitle(path):
     return titleList
 
 
-def GetTxt(dataList, localTitle):
+def GetTxt(dataList, localTitle, path):
     fileTitle = localTitle + ".txt"  # 合成.txt格式 文件名
-
+    fileTitle = os.path.join(path, fileTitle)
     nameFile = open(fileTitle, "w", encoding="utf-8")  # 写入文件
     j = 0
     for item in dataList:
@@ -148,16 +157,26 @@ def GetTxt(dataList, localTitle):
 def DeleteTxt(delDir, delName):
     delList = os.listdir(delDir)
     for f in delList:
-        if f == delName:
+        if os.path.join(delDir, f) == delName:
             filePath = os.path.join(delDir, f)
             if os.path.isfile(filePath):
                 os.remove(filePath)
 
 
 def DeleteDir(delDir):
-    os.system(f"attrib -r {delDir}")
+    # 文件夹 带 - 的会删不掉,但是文件还是删的掉的
+    os.system(f"attrib -r {delDir}")  # 增加可对文件夹产生修改的权限
     shutil.rmtree(delDir, True)
 
 
-def RenameDir(name):
-    os.rename()
+# 在指定的输出文件夹下面创建名称为name的文件夹
+def MakeDir(path, name):
+    dir = os.path.join(path, name)
+
+    if not os.path.exists(dir):
+        os.makedirs(dir)
+    else:
+        dir = os.path.join(path, name + str(random.randint(0, 100)))
+        os.makedirs(dir)
+
+    return dir