遍历并建立html链接

    技术2025-06-25  12

    import os, string root_dir = 'd:/Downloads' html = '.html' def array_to_html(files, file_name): html_file = open(file_name + '.html', 'w') for f in files: l = f.split('/') html_file.write('<a href="file:///' + f + '">' + l[-1] + '</a><br />') html_file.close() def process_dir(cur_dir): if os.path.exists(cur_dir): print cur_dir if not os.path.isfile(cur_dir): files = os.listdir(cur_dir) contents = list() for item in files: if os.path.isfile(cur_dir + '/' + item): contents.append(cur_dir + '/' + item) else: process_dir(cur_dir + '/' + item) array_to_html(contents, cur_dir) else: pass #should deal with the other html files if __name__ == "__main__": process_dir(root_dir)
    最新回复(0)