Open In App

How to remove all .pyc files in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to remove all .pyc files in Python.

What is a .pyc file?

A *.pyc file is created by the Python interpreter when a *.py file is imported into any other python file. The *.pyc file contains the “compiled bytecode” of the imported module/program so that the “translation” from source code to bytecode can be skipped on subsequent imports of the *.py file. Having a *.pyc file saves the compilation time of converting the python source code to byte code, every time the file is imported. Thus speeding startup a little. But it’s still interpreted. Once the *.pyc file is generated, there is no need for the *.py file, unless the *.py file is modified. In Python 3.2, the compiled files (.pyc) are placed in a __pycache__ subdirectory and are named differently depending on which Python interpreter created them.

How the .pyc file is maintained? 

Before executing a python program python interpreter checks for the .pyc file. If the file is present, the virtual machine executes it. If not found, it checks for the .py file. If found, compile it to a .pyc file and then the python virtual machine executes it.

while working on a project having the extra .pyc file unnecessarily increases the file size. or to solve any terrible importing error, the .pyc are needed to be removed.

Steps to delete all .pyc files: 

On OS X and Linux find method can be used to find all of the .pyc files, and then use its delete option to delete them. (In windows open windows terminal to use the find method).

The command to find all .pyc files in all folders, starting with the current one is:

find. -name '*.pyc'

If you want to delete all the files found, just add the -delete option:

find.  -name '*.pyc' -delete

Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads