def visitDir_walk(path , sFileSuffix): codedetect = chardet.detect(path)["encoding"] path = unicode(path , codedetect) fileNames = list() for root,dirs,files in os.walk(path): for i in range(0 , len(files)): file = files if file.endswith(sFileSuffix): sFileName = os.path.join(root , file) #这边返回的应该是包含视频文件的父目录,也就是学生作品目录 realFileName = os.path.split(os.path.abspath(sFileName))[0] fileNames.append( unicode(realFileName,”gbk”) ) return fileNames def main(): sProductionInputPath = “/home/chaoma/superboard/mydata/input/production” dir_production_list = visitDir_walk(sProductionInputPath , ".mp4") #拼接name.txt的路径 for dir in dir_production_list: nameFile = os.path.join(dir , "name.txt") fout = codecs.open(nameFile , "w" , "utf-8") #截取出路径中的文件名作为需要的内容,并写入文件 fout.write(os.path.split(path)[1]) fout.close() main() |