🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
fastdfs提供的脚本不能列出所有的文件,所以我自己写了一个python脚本遍历出所有文件 pythonToRedis.py 把所有文件写入到了本地文件,当然也可以存入关系型数据库或者Redis中(用set去重,做一些交差补运算) ## 1. 脚本 ~~~ import re import sys import os def allFiles(): path = '/data/fastdfs/storage/data' # basepath配置路径 rounds = 1 fdfspath = 'group1/M00' with open('/home/timing/shelles/data.txt','w') as file_url: for dirpath, dirnames, filenames in os.walk(path): if rounds == 1: rounds+=1 elif (dirpath == path + '/sync'): continue else: for file in filenames: try: paths = re.search(r'/data/fastdfs/storage/data(.*)',dirpath).group(1) fullpath = os.path.join(fdfspath + paths, file) print(fullpath) file_url.write(fullpath + '\n') except: pass rounds+=1 file_url.close() def toRedis(): with open('/home/redis/tuna/shelles/data.txt', 'r') as logfile: for line in logfile: print(line) redis_client.sadd('dfs_picture',line.replace('\n', '')) logfile.close() if __name__ == '__main__': if(sys.argv[1] == 'allfiles'): allFiles() elif(sys.argv[1] == 'toredis'): toRedis() else: print("USAGE:allfiles|toredis") ~~~ ## 2. 脚本说明 ~~~ # dirpath:当前路径,dirnames:当前路径下的文件夹,filenames:当前路径下的文件 for dirpath, dirnames, filenames in os.walk(path): ~~~ os.walk(path)返回一个生成器,生成单位是: (当前路径,当前路径当前路径下的文件夹-List,当前路径下的文件-List)元组 ![](https://box.kancloud.cn/9984b7c73234b79e582ad02161eaa1e4_777x176.png) ![](https://box.kancloud.cn/9cf5527da654d002eb55630cdf974028_603x259.png)