编程之美-Pyhon

Python3-批量转换less为css

2018-05-13  本文已影响0人  神木惊蛰

转换前:

转换前

转换中:

转换中

转换后:

转换后

源码

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()

作者:木子昭
链接:https://www.jianshu.com/p/27229441f5c0
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇下一篇

猜你喜欢

热点阅读