Python Forum
program wanted: clean up pyc files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html)
+--- Forum: Bar (https://python-forum.io/forum-27.html)
+--- Thread: program wanted: clean up pyc files (/thread-3680.html)



program wanted: clean up pyc files - Skaperen - Jun-13-2017

i would like to find a program/script (in python) that scans for .pyc files and lists or removes them if the .py file for them exists and is readable. this should know how to work with python3's way of doing things. any issues i should know about?


RE: program wanted: clean up pyc files - wavic - Jun-13-2017

Once a .py file is executed a .pyc file is generated. I am not sure if this always happens


RE: program wanted: clean up pyc files - Larz60+ - Jun-13-2017

you can write one pretty easily using os.walk.
There is one for django, that you can probably modify
django-pyc (search for it in PyPi)

also a search for 'remove .pyc' in PyPi returns about a dozen packages


RE: program wanted: clean up pyc files - DeaD_EyE - Jun-13-2017

On Linux you can just use the shell.

find /path/to/app -name '*.pyc' -delete


RE: program wanted: clean up pyc files - snippsat - Jun-13-2017

As mention is pretty easy to write this with os.walk().
os.walk() has also gotten a lot faster in Python 3.5-->.
scandir has been taken into standard library.
Quote:In practice, removing all those extra system calls makes os.walk() about 7-50 times as fast on Windows,
and about 3-10 times as fast on Linux and Mac OS X.
So we're not talking about micro-optimizations.
import os

source = '/foo'
for root,dirs,files in os.walk(source):
   for f_name in files:
       if f_name.endswith(('.pyc', '.txt')):
           f_name = os.path.join(root, f_name)
           print(f_name) # Test to see that's it okay first 
           #os.remove(f_name)



RE: program wanted: clean up pyc files - nilamo - Jun-13-2017

If you use git, a common practice (and the default for python on github) is to have .pyc and the __pycache__ folder in the .gitignore file, so the files are never added to source control. That way when it comes to distribution, those files are never packaged or included for other people. Depending on what you're doing, then, "getting rid" of the files could be as easy as pulling the repo into a new folder.


RE: program wanted: clean up pyc files - snippsat - Jun-13-2017

Yes .gitignore file is fine way to clean up.
These command clean up online repo if writing a .gitignore file later.
git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"