FileOperator.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # -*- coding = utf-8 -*-
  2. # @Time : 2022/6/22 12:29
  3. # @Author : 刘正阳
  4. # @File : FileOperator.py
  5. # @Software : PyCharm
  6. import os
  7. import random
  8. import re
  9. import shutil
  10. '''
  11. 传入path值,读取当前path一级目录下的.dvi文件,返回[isDviFounded,bid,aid,title]列表
  12. '''
  13. global localFileName
  14. def GetDviInfo(path):
  15. isDviFounded = False
  16. file_type = '.dvi'
  17. dviFile = None
  18. bid = None
  19. aid = None
  20. title = None
  21. description = None
  22. filelist = os.listdir(path)
  23. for file in filelist:
  24. if file_type in file:
  25. isDviFounded = True
  26. dviFile = os.path.join(path, file)
  27. if isDviFounded is False:
  28. return [isDviFounded, bid, aid, title]
  29. else:
  30. with open(dviFile, encoding='UTF-8') as f:
  31. lines = f.readlines()
  32. s = str(lines[0])
  33. findBid = re.compile(r'"Bid":"(.*?)"')
  34. findDviTitle = re.compile(r'"Title":"(.*?)"')
  35. findAid = re.compile(r'"Aid":"(.*?)"')
  36. bid = re.findall(findBid, s)[0]
  37. aid = re.findall(findAid, s)[0]
  38. title = re.findall(findDviTitle, s)[0]
  39. for s in title:
  40. cut = ['|', '\\', '/', ':', '?', '"', '<', '>']
  41. if s in cut:
  42. title = title.replace(s, ' ')
  43. return [isDviFounded, bid, aid, title]
  44. def GetFileSeries(fileList):
  45. return int(fileList.split('\\')[-2])
  46. def FindAllMp4Files(path):
  47. # 这里是不需要对输出结果排序的,因为在移动这些文件后,DoRename调用被移动的文件,会排好序
  48. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  49. fileList = [] # 存储要copy的文件全名
  50. fileNamelist = []
  51. for dirPath, dirNames, fileNames in os.walk(path):
  52. for file in fileNames:
  53. fileType = file.split('.')[-1]
  54. if fileType in fileTypeList:
  55. file_fullname = os.path.join(dirPath, file) # 文件全名
  56. fileList.append(file_fullname)
  57. fileNamelist.append(file)
  58. return [fileList, fileNamelist]
  59. def FindSpecialMp4Files(path, aID):
  60. # 提取出含有指定特征的fileList
  61. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  62. fileList = [] # 存储要copy的文件全名
  63. fileNameList = []
  64. # 获取要被命名的文件,包含了文件夹有其它文件或文件夹的情况
  65. for dirPath, dirNames, fileNames in os.walk(path):
  66. for file in fileNames:
  67. if aID in file and file.split('.')[-1] in fileTypeList: #
  68. oldName = os.path.join(dirPath, file) # 文件全名
  69. if os.path.isdir(oldName):
  70. continue
  71. fileList.append(oldName)
  72. fileNameList.append(file)
  73. return [fileList, fileNameList]
  74. # 检测文件是否加密 是则解密
  75. def DecryptMp4(path, aID):
  76. isEncrypted = None
  77. s = None
  78. encryptedFile = None
  79. decryptedFile = None
  80. countEncChar = 0 # 检测'xff'数量
  81. countDecChar = 0 # 检测'x00'数量
  82. fileList = FindSpecialMp4Files(path, aID)[0]
  83. testFile = fileList[0]
  84. with open(testFile, "rb") as f:
  85. s = str(f.readline())[3:14]
  86. f.close()
  87. sList = s.split('\\') # ['xff', 'xff', 'xff']
  88. for item in sList:
  89. if 'xff' in item:
  90. countEncChar += 1
  91. if 'x00' in item:
  92. countDecChar += 1
  93. if countEncChar == 3:
  94. isEncrypted = True # 加密
  95. if countDecChar == 3:
  96. isEncrypted = False # 未加密
  97. if isEncrypted is None:
  98. return
  99. if not isEncrypted: # 如果未加密
  100. pass
  101. else: # 如果加密则解密
  102. for file in fileList:
  103. encryptedFile = open(file, 'rb')
  104. encryptedFile.seek(3)
  105. byte = encryptedFile.read()
  106. with open(file, 'wb') as decryptedFile:
  107. decryptedFile.write(byte)
  108. encryptedFile.close()
  109. decryptedFile.close()
  110. # return fileList
  111. def CopyFile(srcFileList, dstFolder):
  112. for file in srcFileList:
  113. shutil.copy(file, dstFolder)
  114. def MoveFile(srcFileList, dstFolder):
  115. for file in srcFileList:
  116. shutil.move(file, dstFolder)
  117. # 排序用到的key
  118. def GetSeries(dataList):
  119. return int(dataList.split('_')[-2])
  120. def DoRename(path, fileName, aID):
  121. # 获取.txt文件名
  122. filName = fileName
  123. # 读取.txt文件
  124. with open(filName, encoding='UTF-8') as f:
  125. lines = f.readlines() # 新文件名按行保存
  126. fileList = FindSpecialMp4Files(path, aID)[0] # 存储要copy的文件全名
  127. fileList.sort(key=GetSeries)
  128. # fileList = set(fileList) # 防止文件重复
  129. index = 0
  130. frontIndex = 0
  131. for oldDir in fileList:
  132. filetype = '.' + oldDir.split('.')[-1]
  133. frontIndex = int(oldDir.split('_')[-2])
  134. newDir = os.path.join(path, str(frontIndex) + '. ' + lines[index].strip('\n') + filetype) # 新的文件路径
  135. index += 1
  136. os.rename(oldDir, newDir) # 重命名
  137. def GetInfoList(path, aID):
  138. fileTypeList = aID + '.info'
  139. fileList = [] # 含路径的文件名
  140. for dirPath, dirNames, fileNames in os.walk(path):
  141. for file in fileNames:
  142. if file == fileTypeList:
  143. file_fullname = os.path.join(dirPath, file) # 文件名
  144. fileList.append(file_fullname)
  145. fileList.sort(key=GetFileSeries) # 这里必须排序
  146. return fileList
  147. def GetLocalVideoTitle(path, aID):
  148. fileList = GetInfoList(path, aID)
  149. #  print(fileList)
  150. titleList = []
  151. findVideoTitle = re.compile(r'"PartName":"(.*?)"')
  152. for infoFile in fileList:
  153. with open(infoFile, encoding='UTF-8') as f:
  154. lines = f.readlines()
  155. s = str(lines[0])
  156. videoTitle = re.findall(findVideoTitle, s)[0]
  157. titleList.append(videoTitle)
  158. return titleList
  159. def GetTxt(dataList, localTitle, path):
  160. fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
  161. fileTitle = os.path.join(path, fileTitle)
  162. nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
  163. j = 0
  164. for item in dataList:
  165. j += 1
  166. nameFile.write(item + "\n")
  167. nameFile.close()
  168. return fileTitle
  169. def DeleteTxt(delDir, delName):
  170. delList = os.listdir(delDir)
  171. for f in delList:
  172. if os.path.join(delDir, f) == delName:
  173. filePath = os.path.join(delDir, f)
  174. if os.path.isfile(filePath):
  175. os.remove(filePath)
  176. def DeleteDir(delDir):
  177. # 文件夹 带 - 的会删不掉,但是文件还是删的掉的
  178. os.system(f"attrib -r {delDir}") # 增加可对文件夹产生修改的权限
  179. shutil.rmtree(delDir, True)
  180. # 在指定的输出文件夹下面创建名称为name的文件夹
  181. def MakeDir(path, name):
  182. dir = os.path.join(path, name)
  183. if not os.path.exists(dir):
  184. os.makedirs(dir)
  185. else:
  186. dir = os.path.join(path, name + str(random.randint(0, 100)))
  187. os.makedirs(dir)
  188. return dir