需求:
最近有个需求,需求对目录下的一个json数据进行处理,主要对json里面的title如果为空字符串,将此文件进行删除,如果文件夹为空,也就行递归的查找和删除。

直接上我的代码,需要自己拿去,O(∩_∩)O哈哈~

import json
	import os
	
	def remove_none_dirs(path):
	    # 去重空文件夹
	    list = os.listdir(path)
	    for i in list:
	        path_new = os.path.join(path, i)
	        if os.path.isdir(path_new):
	            list2 = os.listdir(path=path_new)
	            if len(list2) > 0:
	                remove_none_dirs(path_new)
	            else:
	                os.removedirs(path_new)
	        else:
	            break
	
	
	def remove_json_error(path):
	    # 去重json脏数据,title为空的
	    list = os.listdir(path)
	    for i in list:
	        path_new = os.path.join(path, i)
	        if os.path.isdir(path_new):
	            remove_json_error(path_new)
	        else:
	            if path_new.endswith("json"):
	                title = add_file_to_redis(path_new)
	                if title.strip() == "":
	                    os.remove(path_new)
	                else:
	                    pass
	            else:
	                pass
	
	
	def add_file_to_redis(path):
	    with open(path, 'r', encoding='utf-8') as file:
	        ret = file.read()
	    data = json.loads(ret)
	    title = data["Title"]
	    print(title)
	    return title
	
	
	if __name__ == '__main__':
	    path = r"需要查找的目录"
	    remove_json_error(path)
	    remove_none_dirs(path)