Python Forum
program wanted: clean up pyc files
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
program wanted: clean up pyc files
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Once a .py file is executed a .pyc file is generated. I am not sure if this always happens
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
On Linux you can just use the shell.

find /path/to/app -name '*.pyc' -delete
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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)
Reply
#6
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.
Reply
#7
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  program wanted in python Skaperen 2 2,658 Aug-07-2018, 12:05 AM
Last Post: Skaperen
  program wanted: diff that ignores numbers Skaperen 0 1,888 Jun-16-2018, 02:05 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020