Muneeb
Muneeb

Reputation: 297

python running lessc command

I have the following command lessc lessc xyz.less > xyz.css

I want to run that command in python for which i have written this code

   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)

but it gives error windowerror 2 cannot find the path specifies

Upvotes: 0

Views: 643

Answers (1)

Jeremy Allen
Jeremy Allen

Reputation: 6644

Make sure that 'lessc' is in your path, you could try using the full path to lessc instead.

You don't need to use shell style redirection with Popen like this, check the subprocess.Popen docs

Here is an example of how to do it without shell redirection:

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

Upvotes: 1

Related Questions