【Python】创建目录名时包含包含特殊符号的处理
特殊符号处理
# coding:utf-8 import os class make_folder: @staticmethod def __special_symbol_processing(folder_name): """目录名称中的特殊符号处理""" char_list = ['*', '|', ':', '?', '/', '<', '>', '"', '\\'] news_title_result_list = [] for i in folder_name: if i in char_list: news_title_result_list.append('') else: news_title_result_list.append(i) news_title_result = ''.join(news_title_result_list) # print("新的标题名:{}".format(news_title_result)) return news_title_result @staticmethod def __create_folder(folder_path): """ 递归创建目录 :param folder_path: 创建目录位置 :return: """ os.makedirs(folder_path, exist_ok=True) # exist_ok=True 允许已存在的目录创建 print("创建的文件夹:{}".format(folder_path)) def __init__(self, folder_name): new_folder_name = self.__special_symbol_processing(folder_name) self.__create_folder(new_folder_name) if __name__ == "__main__": """run""" make_folder("*创建的目录/abc") make_folder("创建的<目录>|mnk")
执行结果
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------