标签:for open 获取 try 命令行 color span inf pen
1 import os 2 3 def auto_less_to_css(file_dir): 4 # 获取当前目录下所有的css文件路径 5 all_whole_path_files = [] 6 for root, dirs, files in os.walk(file_dir): 7 for file in files: 8 try: 9 if file[-5:] == ".less": 10 file_info = [root+‘/‘, file] 11 all_whole_path_files.append(file_info) 12 except Exception as e: 13 print(e) 14 15 for file_info in all_whole_path_files: 16 less_file_path_file = file_info[0] + file_info[1] 17 css_file_name = file_info[1][:-5] + ‘.css‘ 18 css_file_path_file = file_info[0] + css_file_name 19 new_command = ‘lessc ‘ + less_file_path_file + ‘ > ‘ + css_file_path_file 20 21 try: 22 result = os.popen(new_command).readlines() 23 if len(result) == 0: 24 print(less_file_path_file, "已经转换为", css_file_path_file) 25 except Exception as e: 26 print(e) 27 28 def main(): 29 auto_less_to_css(‘.‘) 30 31 if __name__ == ‘__main__‘: 32 main()
python 写脚本很方便 此脚本功能为将某文件夹内的所有.less文件转换为.css文件 此脚本来源于js 木子昭
理论上利用os.walk()便利所有文件夹路径 文件夹名称 文件名称
字符串拼接和切片
加上os.popen(new_command).readlines() 命令行操作
可以执行所有的的批量文件转换功能(前提是已经安装了转换器,可以cmd命令调用)
标签:for open 获取 try 命令行 color span inf pen
原文地址:https://www.cnblogs.com/monstermr/p/10590300.html