使用正则表达式保留数字、字母和反斜杠\

# coding: utf8

import hashlib
import os
import sys
import re
from collections import Counter

reload(sys)
sys.setdefaultencoding('utf-8')

if __name__ == "__main__":
    output_list=[]
    output_path=os.getcwd()

    for path,dir_list,file_list in os.walk(output_path) :  
        for dir_name in dir_list:
            output_list.append(''.join(re.findall(r'[a-zA-Z0-9]|\\|:',os.path.join(path, dir_name)) ).replace(output_path, ""))

    for i in output_list:
        with open('counts_path.log', mode='a+') as f:
            f.write(i+'\n')
        print i

你可能感兴趣的:(代码片段)