FileOperator.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 file_name
  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 FindAllMp4Files(path):
  44. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  45. fileList = [] # 存储要copy的文件全名
  46. fileNamelist = []
  47. for dirPath, dirNames, fileNames in os.walk(path):
  48. for file in fileNames:
  49. fileType = file.split('.')[-1]
  50. if fileType in fileTypeList:
  51. file_fullname = os.path.join(dirPath, file) # 文件全名
  52. fileList.append(file_fullname)
  53. fileNamelist.append(file)
  54. return [fileList, fileNamelist]
  55. # return fileList
  56. def CopyFile(srcFileList, dstFolder):
  57. for file in srcFileList:
  58. shutil.copy(file, dstFolder)
  59. def MoveFile(srcFileList, dstFolder):
  60. for file in srcFileList:
  61. shutil.move(file, dstFolder)
  62. # 排序用到的key
  63. def get_series(dataList):
  64. return int(dataList.split('_')[1])
  65. def DoRename(path, fileName):
  66. # 获取文件名
  67. filName = fileName
  68. # 读取该文件
  69. with open(filName, encoding='UTF-8') as f:
  70. lines = f.readlines() # 新文件名按行保存
  71. fileList = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
  72. fileList.sort(key=get_series)
  73. i = 1
  74. for files in fileList: # 遍历所有文件
  75. oldDir = os.path.join(path, files) # 原来的文件路径
  76. if os.path.isdir(oldDir): # 如果是文件夹则跳过
  77. continue
  78. filetype = os.path.splitext(files)[1] # 文件扩展名
  79. newDir = os.path.join(path, str(i) + '. ' + lines[i].strip('\n') + filetype) # 新的文件路径
  80. os.rename(oldDir, newDir) # 重命名
  81. i = i + 1
  82. def DeleteTxt(delDir, delName):
  83. delList = os.listdir(delDir)
  84. for f in delList:
  85. if f == delName:
  86. filePath = os.path.join(delDir, f)
  87. if os.path.isfile(filePath):
  88. os.remove(filePath)
  89. def DeleteDir(delDir):
  90. os.system(f"attrib -r {delDir}")
  91. shutil.rmtree(delDir, True)
  92. def RenameDir(name):
  93. os.rename()