leftover pyc files

Chris Kaynor ckaynor at zindagigames.com
Wed Nov 2 12:06:37 EDT 2011


If you can at least upgrade to Python 2.6, another option, if you
don't mind losing the byte code caching all together (it may worsen
load times, however probably not significantly), you can set
PYTHONDONTWRITEBYTECODE to prevent the writing of .pyc files.
Alternatively, again in 2.6+, you can also specify the -B command-line
argument. See http://docs.python.org/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
and http://docs.python.org/using/cmdline.html#cmdoption-unittest-discover-B

Chris



On Wed, Nov 2, 2011 at 8:45 AM, Andrea Crotti <andrea.crotti.0 at gmail.com> wrote:
> On 11/02/2011 03:03 PM, Peter Otten wrote:
>>
>> Switch to Python3.2 ;)
>
> Yes I saw that and it would be great, unfortunately we're stuck with Python
> 2.5 :O
> for some more time.
>
> Anyway now the code that does it is a recursive thing ilke
> def _clean_orphaned_pycs(self, directory, simulate=False):
>    """Remove all the pyc files without a correspondent module
>    """
>    files = listdir(directory)
>    # going down the tree recursively (like os.walk)
>    for x in files:
>        if x.endswith('.pyc'):
>            py_file = x.split('.pyc')[0] + '.py'
>
>            if (not simulate) and (py_file not in files):
>                deletable_pyc = path.join(directory, x)
>                print 'DELETING pyc %s as it has no py %s' % \
>                    (deletable_pyc, py_file)
>                remove(deletable_pyc)
>
>        #TODO: move out these special cases
>        if path.isdir(path.join(directory, x)) \
>            and x != '.svn' \
>            and not x.endswith('.egg-info'):
>
>            self._clean_orphaned_pycs(path.join(directory, x))
>
>
> Can be done much better probably with os.walk or os.path.walk,
> any suggestions?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list