- 安裝了less之后后, 可以通過
lessc
將單個.less
文件轉(zhuǎn)換為.css
, 但less沒有提供批量轉(zhuǎn)換為css的命令, 如果不使用sublime, 或者webstorm提供的插件, 就得手動一個一個去轉(zhuǎn)換 - 懶是第一生產(chǎn)力, 我用python寫了一個批量轉(zhuǎn)換的腳本, 可以將腳本放到項目的頂層目錄運行, 之后, 就可以在less文件的旁邊找到新生成的css文件
轉(zhuǎn)換前:
轉(zhuǎn)換中:
轉(zhuǎn)換后:
源碼
import os
def auto_less_to_css(file_dir):
# 獲取當(dāng)前目錄下所有的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, "已經(jīng)轉(zhuǎn)換為", css_file_path_file)
except Exception as e:
print(e)
def main():
auto_less_to_css('.')
if __name__ == '__main__':
main()
作者:木子昭
鏈接:http://www.reibang.com/p/27229441f5c0
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)斜筐,非商業(yè)轉(zhuǎn)載請注明出處。