"""
After extracting the RAR, we run this to move all the files into
the appropriate train/test folders.
Should only run this file once!
"""
import os
from os.path import exists
import shutil
from pathlib import Path
def get_train_test_lists(version='01'):
"""
Using one of the train/test files (01, 02, or 03), get the filename
breakdowns we'll later use to move everything.
"""
test_file = os.path.join('ucfTrainTestlist', 'testlist' + version + '.txt')
train_file = os.path.join('ucfTrainTestlist', 'trainlist' + version + '.txt')
with open(test_file) as fin:
test_list = [row.strip() for row in list(fin)]
with open(train_file) as fin:
train_list = [row.strip() for row in list(fin)]
train_list = [row.split(' ')[0] for row in train_list]
file_groups = {
'train': train_list,
'test': test_list
}
return file_groups
def move_files(file_groups):
"""This assumes all of our files are currently in _this_ directory.
So move them to the appropriate spot. Only needs to happen once.
"""
for group, videos in file_groups.items():
for video in videos:
parts = os.path.split(video)
classname = parts[0]
absolutepathname = ('D:/expressionDatabase/UCF101/')
filename = parts[1]
print('文件名:' + filename)
'''
if not os.path.exists(os.path.join(group, filename)):
print("Creating folder for %s/%s" % (group, filename))
os.makedirs(os.path.join(group, filename))
'''
if not exists(absolutepathname + group + '/' + classname):
os.makedirs(absolutepathname + group + '/' + classname)
file_to_move = absolutepathname + 'UCF101/' + classname + '/' + filename
if not os.path.exists(file_to_move):
print("找不到要移动的文件.")
continue
dest = Path(absolutepathname + group + '/' + classname + '/' + filename)
print('目标路径:', dest)
shutil.move(file_to_move, dest)
print('move finished...')
print("Done.")
def main():
"""
Go through each of our train/test text files and move the videos
to the right place.
"""
group_lists = get_train_test_lists()
print(group_lists)
move_files(group_lists)
if __name__ == '__main__':
main()