|
@@ -4,6 +4,7 @@
|
|
# @File : FileOperator.py
|
|
# @File : FileOperator.py
|
|
# @Software : PyCharm
|
|
# @Software : PyCharm
|
|
import os
|
|
import os
|
|
|
|
+import random
|
|
import re
|
|
import re
|
|
import shutil
|
|
import shutil
|
|
|
|
|
|
@@ -81,47 +82,55 @@ def MoveFile(srcFileList, dstFolder):
|
|
|
|
|
|
# 排序用到的key
|
|
# 排序用到的key
|
|
def GetSeries(dataList):
|
|
def GetSeries(dataList):
|
|
- return int(dataList.split('_')[1])
|
|
|
|
|
|
+ return int(dataList.split('_')[-2])
|
|
|
|
|
|
|
|
|
|
-def DoRename(path, fileName):
|
|
|
|
|
|
+def DoRename(path, fileName, aID):
|
|
# 获取.txt文件名
|
|
# 获取.txt文件名
|
|
filName = fileName
|
|
filName = fileName
|
|
# 读取.txt文件
|
|
# 读取.txt文件
|
|
with open(filName, encoding='UTF-8') as f:
|
|
with open(filName, encoding='UTF-8') as f:
|
|
lines = f.readlines() # 新文件名按行保存
|
|
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 = [] # 含路径的文件名
|
|
fileList = [] # 含路径的文件名
|
|
|
|
|
|
for dirPath, dirNames, fileNames in os.walk(path):
|
|
for dirPath, dirNames, fileNames in os.walk(path):
|
|
for file in fileNames:
|
|
for file in fileNames:
|
|
- fileType = file.split('.')[-1]
|
|
|
|
- if fileType in fileTypeList:
|
|
|
|
|
|
+ if file == fileTypeList:
|
|
file_fullname = os.path.join(dirPath, file) # 文件名
|
|
file_fullname = os.path.join(dirPath, file) # 文件名
|
|
fileList.append(file_fullname)
|
|
fileList.append(file_fullname)
|
|
fileList.sort(key=GetFileSeries) # 这里必须排序
|
|
fileList.sort(key=GetFileSeries) # 这里必须排序
|
|
return fileList
|
|
return fileList
|
|
|
|
|
|
|
|
|
|
-def GetLocalVideoTitle(path):
|
|
|
|
- fileList = GetInfoList(path)
|
|
|
|
- print(fileList)
|
|
|
|
|
|
+def GetLocalVideoTitle(path, aID):
|
|
|
|
+ fileList = GetInfoList(path, aID)
|
|
|
|
+ # print(fileList)
|
|
titleList = []
|
|
titleList = []
|
|
findVideoTitle = re.compile(r'"PartName":"(.*?)"')
|
|
findVideoTitle = re.compile(r'"PartName":"(.*?)"')
|
|
for infoFile in fileList:
|
|
for infoFile in fileList:
|
|
@@ -133,9 +142,9 @@ def GetLocalVideoTitle(path):
|
|
return titleList
|
|
return titleList
|
|
|
|
|
|
|
|
|
|
-def GetTxt(dataList, localTitle):
|
|
|
|
|
|
+def GetTxt(dataList, localTitle, path):
|
|
fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
|
|
fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
|
|
-
|
|
|
|
|
|
+ fileTitle = os.path.join(path, fileTitle)
|
|
nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
|
|
nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
|
|
j = 0
|
|
j = 0
|
|
for item in dataList:
|
|
for item in dataList:
|
|
@@ -148,16 +157,26 @@ def GetTxt(dataList, localTitle):
|
|
def DeleteTxt(delDir, delName):
|
|
def DeleteTxt(delDir, delName):
|
|
delList = os.listdir(delDir)
|
|
delList = os.listdir(delDir)
|
|
for f in delList:
|
|
for f in delList:
|
|
- if f == delName:
|
|
|
|
|
|
+ if os.path.join(delDir, f) == delName:
|
|
filePath = os.path.join(delDir, f)
|
|
filePath = os.path.join(delDir, f)
|
|
if os.path.isfile(filePath):
|
|
if os.path.isfile(filePath):
|
|
os.remove(filePath)
|
|
os.remove(filePath)
|
|
|
|
|
|
|
|
|
|
def DeleteDir(delDir):
|
|
def DeleteDir(delDir):
|
|
- os.system(f"attrib -r {delDir}")
|
|
|
|
|
|
+ # 文件夹 带 - 的会删不掉,但是文件还是删的掉的
|
|
|
|
+ os.system(f"attrib -r {delDir}") # 增加可对文件夹产生修改的权限
|
|
shutil.rmtree(delDir, True)
|
|
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
|