当前目录下所有文件夹下的文件名(不带后缀)写入对应txt文件(以文件夹命名)中
重命名文件
getfiledata.py
import os
def Makedir(path):
folder = os.path.exists(path)
if (not folder):
os.makedirs(path)
def GetFileName(fileDir, outDir):
list_name = []
Makedir(outDir)
for dir in os.listdir(fileDir):
filePath = os.path.join(fileDir, dir)
if os.path.isdir(filePath) and not (filePath == outDir):
txt = outDir + dir + ".txt"
for root, subDir, files in os.walk(filePath):
for fileName in files:
f = open(txt, 'a')
fileName = os.path.splitext(fileName)[0] + '\n'
f.write(fileName)
f.close()
def main():
fileDir = "H:\\resultdata"
outDir = "H:\\resultdata\\FileData\\"
files = GetFileData(fileDir, outDir)
if __name__ == "__main__":
main()
single_renamefile.py
import os
def GetVideoName(dir):
listName = []
for fileName in os.listdir(dir):
if os.path.splitext(fileName)[1] == '.mp4':
fileName = os.path.splitext(fileName)[0]
listName.append(fileName)
return listName
def GetTxtName(dir):
listName = []
for fileName in os.listdir(dir):
if os.path.splitext(fileName)[1] == '.txt':
fileName = os.path.splitext(fileName)[0]
listName.append(fileName)
return listName
def IsPart(frames, video):
length = len(frames)
for i in range(length):
if video == frames[i]:
return True
else:
continue
return False
def ReadVideoInfo(path):
f = open(path, 'r')
fileContents = []
for fileContent in f.readlines():
fileContent = fileContent.strip("\n")
fileContents.append(fileContent)
return fileContents
def ReadLabel(labelFile):
f = open(labelFile, 'r')
labelContents = []
for line in f.readlines():
labelContents.append(line)
return labelContents
time_9 = 0
time_9_11 = 0
time_11_13 = 0
time_13_15 = 0
time_15_17 = 0
time_17 = 0
def GetTimeNum(string):
strTmp = string.split('_')
strTmp = strTmp[1]
number = int(strTmp)
global time_9
global time_9_11
global time_11_13
global time_13_15
global time_15_17
global time_17
if number < 900:
time_9 += 1
elif number >= 900 and number < 1100:
time_9_11 += 1
elif number >= 1100 and number < 1300:
time_11_13 += 1
elif number >= 1300 and number < 1500:
time_13_15 += 1
elif number >= 1500 and number < 1700:
time_15_17 += 1
else:
time_17 += 1
def RenameFile(videoInfoDir, videoDir, labelFile):
videosName = GetVideoName(videoDir)
videoInfoList = GetTxtName(videoInfoDir)
labelContents = ReadLabel(labelFile)
videosNameLen = len(videosName)
for i in range(videosNameLen):
for videoInfo in videoInfoList:
path = videoInfoDir + "\\" + videoInfo + '.txt'
fileContents = ReadVideoInfo(path)
if IsPart(fileContents, videosName[i]):
src = videoDir + '\\' + videosName[i] + '.mp4'
dst = videoDir + '\\' + videoInfo + \
'_' + videosName[i] + '.mp4'
os.rename(src, dst)
labelLine = labelContents[i]
modiLine = videoInfo + '_' + labelLine
GetTimeNum(modiLine)
labelF = open(videoDir+'\\'+'newlabel.txt', 'a')
labelF.write(modiLine)
labelF.close()
break
else:
if videoInfo == videoInfoList[len(videoInfoList) - 1]:
src = videoDir + '\\' + videosName[i] + '.mp4'
dst = videoDir + '\\' + '20170504_1300_A' + \
'_' + videosName[i] + '.mp4'
os.rename(src, dst)
labelLine = labelContents[i]
modiLine = '20170504_1300_A' + '_' + labelLine
GetTimeNum(modiLine)
labelF = open(videoDir+'\\'+'newlabel.txt', 'a')
labelF.write(modiLine)
labelF.close()
def main():
videoDir = "H:\\Video_selected\\single"
labelFile = "H:\\Video_selected\\single\\label.txt"
videoInfoDir = "H:\\Video_selected\\FileData"
RenameFile(videoInfoDir, videoDir, labelFile)
print(time_9,
time_9_11,
time_11_13,
time_13_15,
time_15_17,
time_17)
if __name__ == "__main__":
main()
mul_renamefile.py
import os
def GetVideoName(dir):
listName = []
for fileName in os.listdir(dir):
if os.path.splitext(fileName)[1] == '.mp4':
fileName = os.path.splitext(fileName)[0]
listName.append(fileName)
return listName
def GetTxtName(dir):
listName = []
for fileName in os.listdir(dir):
if os.path.splitext(fileName)[1] == '.txt':
fileName = os.path.splitext(fileName)[0]
listName.append(fileName)
return listName
def IsPart(frames, video):
length = len(frames)
for i in range(length):
if video == frames[i]:
return True
else:
continue
return False
def ReadVideoInfo(path):
f = open(path, 'r')
fileContents = []
for fileContent in f.readlines():
fileContent = fileContent.strip("\n")
fileContents.append(fileContent)
return fileContents
def ReadLabel(labelFile):
f = open(labelFile, 'r')
labelContents = []
for line in f.readlines():
labelContents.append(line)
return labelContents
time_9 = 0
time_9_11 = 0
time_11_13 = 0
time_13_15 = 0
time_15_17 = 0
time_17 = 0
def GetTimeNum(string):
strTmp = string.split('_')
strTmp = strTmp[1]
number = int(strTmp)
global time_9
global time_9_11
global time_11_13
global time_13_15
global time_15_17
global time_17
if number < 900:
time_9 += 1
elif number >= 900 and number < 1100:
time_9_11 += 1
elif number >= 1100 and number < 1300:
time_11_13 += 1
elif number >= 1300 and number < 1500:
time_13_15 += 1
elif number >= 1500 and number < 1700:
time_15_17 += 1
else:
time_17 += 1
def RenameFile(videoInfoDir, videoDir, labelFile):
videosName = GetVideoName(videoDir)
videosNameLen = len(videosName)
totalVideoName = []
for i in range(videosNameLen):
tmp = videosName[i].split('_')
tmp = tmp[0] + '_' + tmp[1]
totalVideoName.append(tmp)
videoInfoList = GetTxtName(videoInfoDir)
labelContents = ReadLabel(labelFile)
for i in range(videosNameLen):
for videoInfo in videoInfoList:
path = videoInfoDir + "\\" + videoInfo + '.txt'
fileContents = ReadVideoInfo(path)
if IsPart(fileContents, totalVideoName[i]):
src = videoDir + '\\' + videosName[i] + '.mp4'
dst = videoDir + '\\' + videoInfo + \
'_' + videosName[i] + '.mp4'
os.rename(src, dst)
labelLine = labelContents[i]
modiLine = videoInfo + '_' + labelLine
GetTimeNum(modiLine)
labelF = open(videoDir+'\\'+'newlabel.txt', 'a')
labelF.write(modiLine)
labelF.close()
break
else:
if videoInfo == videoInfoList[len(videoInfoList) - 1]:
src = videoDir + '\\' + videosName[i] + '.mp4'
dst = videoDir + '\\' + '20170504_1300_A' + \
'_' + videosName[i] + '.mp4'
os.rename(src, dst)
labelLine = labelContents[i]
modiLine = '20170504_1300_A' + '_' + labelLine
GetTimeNum(modiLine)
labelF = open(videoDir+'\\'+'newlabel.txt', 'a')
labelF.write(modiLine)
labelF.close()
def main():
videoDir = "H:\\Video_selected\\multiple"
labelFile = "H:\\Video_selected\\multiple\\label.txt"
videoInfoDir = "H:\\Video_selected\\FileData"
RenameFile(videoInfoDir, videoDir, labelFile)
print(time_9,
time_9_11,
time_11_13,
time_13_15,
time_15_17,
time_17)
if __name__ == "__main__":
main()