python运行lessc命令

时间:2014-07-11 09:41:25

标签: python less

我有以下命令 lessc lessc xyz.less> xyz.css

我想在python中运行该命令,我已经编写了这段代码

   try:
        project_path = settings.PROJECT_ROOT
        less_path = os.path.join(project_path, "static\\less")
        css_path = os.path.join(project_path, "static\\css")
    except Exception as e:
        print traceback.format_exc()
    less_file = [f for f in os.listdir(less_path) if isfile(join(less_path, f))]
    for files in less_file:
        file_name = os.path.splitext(files)[0]
        cmd = '%s\%s > %s\%s' % (less_path, files, css_path, file_name + '.css')
        p = subprocess.Popen(['lessc', cmd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

但它给出错误windowerror 2找不到路径指定

1 个答案:

答案 0 :(得分:0)

确保' lessc'在您的路径中,您可以尝试使用lessc的完整路径。

您不需要像这样使用Popen进行shell样式重定向,请查看subprocess.Popen docs

以下是如何在没有shell重定向的情况下执行此操作的示例:

import subprocess

lessc_command = '/path/to/lessc'
less_file_path = '/path/to/input.less'
css_file_path = '/path/to/output.css'
with open(css_file_path, 'w') as css_file:
    less_process = subprocess.Popen([lessc_command, less_file_path], stdout=css_file)
    less_process.communicate()