python运行lessc命令
我有一个命令:
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 个回答
1
确保你的系统中可以找到'lessc'这个命令,如果找不到的话,可以试试直接使用'lessc'的完整路径。
在使用Popen的时候,不需要像这样使用shell风格的重定向,具体可以查看subprocess.Popen的文档。
下面是一个不使用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()