import os def optimize_css(css: str) -> str: import re minified_with_comments = "".join(list(map( lambda line: re.sub(r'\s*([:{])\s*', r'\1', re.sub(r'^\s*', '', line)), css.split('\n') ))) return re.sub(r'/\*[\s\S]*?\*/', "", minified_with_comments) def optimize_css_file(): root = os.path.dirname(os.getcwd()) static_dir = root + '/undercover/undercover/static/' css = open(static_dir + 'styles.css', 'r').read() minified = optimize_css(css) with open(static_dir + 'styles_min.css', 'w') as minified_file: minified_file.write(minified)