python2 和python3 删除非代码文件及非代码目录
业务验收,需要使用checkmarx对代码进行安全扫描,一般代码库不规范,提交了很多非代码文件夹及文件夹,需要遍历代码库,删除非代码文件、空文件夹、非代码文件夹,python代码如下,2 和3 都可以运行,可以直接再centos 系统执行。
#!/usr/bin/python # -*- coding: utf-8 -*- import os dir_count = 0 file_count =0 # 递归遍历文件夹 def travel_file(file): global file_count for root, dirs, files in os.walk(file): # 文件处理 for f in files: fn = os.path.join(root, f) # print(fn) hz=fn.split(".")[-1] #hz = os.path.splittext(fn)[-1] #保留指定类型的文件 if hz not in ["javasln","project","java","jsp","jsx","jspf","tag","tld","hbs","properties","aspx","ascx","config","xml","cgi","inc","js","htm","html","json"]: os.chmod(fn,0o777) os.remove(fn) file_count = file_count + 1 #print('移除文件: ' + fn) list_to_delete_file_name=["副本","改前"," copy.js"] #删除文件名包含特定字符的文件 for item in list_to_delete_file_name: if item in f: os.chmod(fn,0o777) os.remove(fn) file_count = file_count + 1 print('移除文件: ' + fn) if is_chinese(f): print(fn) def is_chinese(check_str): """ 代码文件名称一般是不含有中文,检查路径是否有中文,支持python2 python3 :param check_str: :return: """ import sys main_python_version = sys.version_info.major if main_python_version == 3: # python3 for ch in check_str: # python2 for ch in f.decode('utf-8'): for ch in check_str: if u'\u4e00' <= ch <= u'\u9fff': return True else: for ch in check_str.decode('utf-8'): if u'\u4e00' <= ch <= u'\u9fff': return True return False def delete_empty_dir(dir): global dir_count if os.path.isdir(dir): for d in os.listdir(dir): if d in ["node_modules"]: # 删除指定文件夹 可以是非空文件夹 import shutil shutil.rmtree(os.path.join(dir, d), ignore_errors=True) dir_count = dir_count + 1 #print('移除指定目录: ' + os.path.join(dir, d)) delete_empty_dir(os.path.join(dir, d)) if not os.listdir(dir): os.rmdir(dir) dir_count = dir_count + 1 #print('移除空目录: ' + dir) if __name__ == '__main__': deal_path = r'/opt/pmo/zhcz-bb/code' deal_path=r'D:\code\sal' deal_path = r'/opt/pmo/zhcz-bb/push_code' travel_file(deal_path) delete_empty_dir(deal_path) print('移除文件总数: ' + str(file_count)) print('移除文件夹总数: ' + str(dir_count))