python下按文件夹内文件名称排序生成html目录

# coding:utf-8

import os,re
def Generate_menu(dir):
    out_file = "RFC_MENU.html"
    title = "RFC MENU"
    head_text1 = "%s"
    head_text2 = '''



''' head_text3 = '%s' head_text4 = '''

''' tail_text = '''

''' list = os.listdir(dir) list.sort(key=sort_key) #print(list) with open(out_file,'w') as f: f.write(head_text1 % (title)) f.write(head_text2) f.write(head_text3 % (title)) f.write(head_text4) for i in list: f.write("%s
" % (dir,i,i)) f.write(tail_text) def sort_key(s): #sort_strings_with_embedded_numbers re_digits = re.compile(r'(\d+)') pieces = re_digits.split(s) # 切成数字与非数字 pieces[1::2] = map(int, pieces[1::2]) # 将数字部分转成整数 return pieces def main(): os.chdir("./") dir = "./RFC" #相对路径 Generate_menu(dir) if __name__ == '__main__': main()

你可能感兴趣的:(python下按文件夹内文件名称排序生成html目录)