安装了less之后后, 可以通过
lessc
将单个.less
文件转换为.css
, 但less没有提供批量转换为css的命令, 如果不使用sublime, 或者webstorm提供的插件, 就得手动一个一个去转换懒是第一生产力, 我用python写了一个批量转换的脚本, 可以将脚本放到项目的顶层目录运行, 之后, 就可以在less文件的旁边找到新生成的css文件
转换前:
转换前
转换中:
转换中
转换后:
转换后
源码
import os def auto_less_to_css(file_dir): # 获取当前目录下所有的css文件路径 all_whole_path_files = [] for root, dirs, files in os.walk(file_dir): for file in files: try: if file[-5:] == ".less": file_info = [root+'/', file] all_whole_path_files.append(file_info) except Exception as e: print(e) for file_info in all_whole_path_files: less_file_path_file = file_info[0] + file_info[1] css_file_name = file_info[1][:-5] + '.css' css_file_path_file = file_info[0] + css_file_name new_command = 'lessc ' + less_file_path_file + ' > ' + css_file_path_file try: result = os.popen(new_command).readlines() if len(result) == 0: print(less_file_path_file, "已经转换为", css_file_path_file) except Exception as e: print(e)def main(): auto_less_to_css('.')if __name__ == '__main__': main()