FileOperator.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 re
  8. import shutil
  9. '''
  10. 传入path值,读取当前path一级目录下的.dvi文件,返回[isDviFounded,bid,aid,title]列表
  11. '''
  12. global localFileName
  13. def GetDviInfo(path):
  14. isDviFounded = False
  15. file_type = '.dvi'
  16. dviFile = None
  17. bid = None
  18. aid = None
  19. title = None
  20. description = None
  21. filelist = os.listdir(path)
  22. for file in filelist:
  23. if file_type in file:
  24. isDviFounded = True
  25. dviFile = os.path.join(path, file)
  26. if isDviFounded is False:
  27. return [isDviFounded, bid, aid, title]
  28. else:
  29. with open(dviFile, encoding='UTF-8') as f:
  30. lines = f.readlines()
  31. s = str(lines[0])
  32. findBid = re.compile(r'"Bid":"(.*?)"')
  33. findDviTitle = re.compile(r'"Title":"(.*?)"')
  34. findAid = re.compile(r'"Aid":"(.*?)"')
  35. bid = re.findall(findBid, s)[0]
  36. aid = re.findall(findAid, s)[0]
  37. title = re.findall(findDviTitle, s)[0]
  38. for s in title:
  39. cut = ['|', '\\', '/', ':', '?', '"', '<', '>']
  40. if s in cut:
  41. title = title.replace(s, ' ')
  42. return [isDviFounded, bid, aid, title]
  43. def GetFileSeries(fileList):
  44. return int(fileList.split('\\')[-2])
  45. def FindAllMp4Files(path):
  46. # 这里是不需要对输出结果排序的,因为在移动这些文件后,DoRename调用被移动的文件,会排好序
  47. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  48. fileList = [] # 存储要copy的文件全名
  49. fileNamelist = []
  50. for dirPath, dirNames, fileNames in os.walk(path):
  51. for file in fileNames:
  52. fileType = file.split('.')[-1]
  53. if fileType in fileTypeList:
  54. file_fullname = os.path.join(dirPath, file) # 文件全名
  55. fileList.append(file_fullname)
  56. fileNamelist.append(file)
  57. return [fileList, fileNamelist]
  58. # return fileList
  59. def CopyFile(srcFileList, dstFolder):
  60. for file in srcFileList:
  61. shutil.copy(file, dstFolder)
  62. def MoveFile(srcFileList, dstFolder):
  63. for file in srcFileList:
  64. shutil.move(file, dstFolder)
  65. # 排序用到的key
  66. def GetSeries(dataList):
  67. return int(dataList.split('_')[1])
  68. def DoRename(path, fileName):
  69. # 获取.txt文件名
  70. filName = fileName
  71. # 读取.txt文件
  72. with open(filName, encoding='UTF-8') as f:
  73. lines = f.readlines() # 新文件名按行保存
  74. fileList = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
  75. fileList.sort(key=GetSeries)
  76. i = 0
  77. for files in fileList: # 遍历所有文件
  78. oldDir = os.path.join(path, files) # 原来的文件路径
  79. if os.path.isdir(oldDir): # 如果是文件夹则跳过
  80. continue
  81. filetype = os.path.splitext(files)[1] # 文件扩展名
  82. newDir = os.path.join(path, str(i+1) + '. ' + lines[i].strip('\n') + filetype) # 新的文件路径
  83. os.rename(oldDir, newDir) # 重命名
  84. i = i + 1
  85. def GetInfoList(path):
  86. fileTypeList = ['info']
  87. fileList = [] # 含路径的文件名
  88. for dirPath, dirNames, fileNames in os.walk(path):
  89. for file in fileNames:
  90. fileType = file.split('.')[-1]
  91. if fileType in fileTypeList:
  92. file_fullname = os.path.join(dirPath, file) # 文件名
  93. fileList.append(file_fullname)
  94. fileList.sort(key=GetFileSeries) # 这里必须排序
  95. return fileList
  96. def GetLocalVideoTitle(path):
  97. fileList = GetInfoList(path)
  98. print(fileList)
  99. titleList = []
  100. findVideoTitle = re.compile(r'"PartName":"(.*?)"')
  101. for infoFile in fileList:
  102. with open(infoFile, encoding='UTF-8') as f:
  103. lines = f.readlines()
  104. s = str(lines[0])
  105. videoTitle = re.findall(findVideoTitle, s)[0]
  106. titleList.append(videoTitle)
  107. return titleList
  108. def GetTxt(dataList, localTitle):
  109. fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
  110. nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
  111. j = 0
  112. for item in dataList:
  113. j += 1
  114. nameFile.write(item + "\n")
  115. nameFile.close()
  116. return fileTitle
  117. def DeleteTxt(delDir, delName):
  118. delList = os.listdir(delDir)
  119. for f in delList:
  120. if f == delName:
  121. filePath = os.path.join(delDir, f)
  122. if os.path.isfile(filePath):
  123. os.remove(filePath)
  124. def DeleteDir(delDir):
  125. os.system(f"attrib -r {delDir}")
  126. shutil.rmtree(delDir, True)
  127. def RenameDir(name):
  128. os.rename()