Skip to content Skip to sidebar Skip to footer

Delete All .py Files Which Dont Have A Corresponding .pyc File

We want to ship a smaller chunk of python distribution to the customer. So, the idea here is to use the existing python distribution in our application and to do all possible tests

Solution 1:

Python script that should do the job, but I'd be curious to know if Python will still work after running it.

import os

pyc_files = []
py_files = []

for root, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if filename.endswith('.pyc'):
            pyc_files.append(os.path.join(root, filename))
        elif filename.endswith('.py'):
            py_files.append(os.path.join(root, filename))

for py_file in py_files:
    if py_file + 'c' not in pyc_files:
        os.remove(py_file)

Post a Comment for "Delete All .py Files Which Dont Have A Corresponding .pyc File"