How to compile all py file to pyc file in a folder by writing a python script?

From the command line to compile all the files present in the current directory and in the subdirectory, you can simply use the following command.

python3 -m compileall . -q

remove -q if you want the list of compiled files.

You can stop recursion in subdirectories by using -l in

python3 -m compileall . -l

You can write script for that as well.

import compileall

compileall.compile_dir('D:/FAS/config', force=True)

Force will rebuild even if timestamps are up-to-date.

You can read more about it here


import compileall

compileall.compile_dir('D:/FAS/config', force=True)

Tags:

Python