3

During my simulation, Python creates folders named __pycache__. Not just one, but many. The __pycache__-folders are almost always created next to the modules that are executed.

But these modules are scattered in my directory. The main folder is called LPG and has a lot of subfolders, which in turn have further subfolders. The __pycache__-folders can occur at all possible places.

At the end of my simulation I would like to clean up and delete all folders named __pycache__ within the LPG-tree.

What is the best way to do this?

Currently, I am calling the function below on simulation end (also on simulation start). However, that is a bit annoying since I specifically have to write down every Path where a __pycache__-folder might occur.

def clearCache():
    """
    Removes generic `__pycache__` .

    The `__pycache__` files are automatically created by python during the simulation.
    This function removes the generic files on simulation start and simulation end.
    """
    try:
        shutil.rmtree(Path(f"{PATH_to_folder_X}/__pycache__"))
    except:
        pass

    try:
        shutil.rmtree(Path(f"{PATH_to_folder_Y}/__pycache__"))
    except:
        pass
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
maxischl
  • 579
  • 1
  • 11
  • 29
  • [This](https://stackoverflow.com/questions/28991015/python3-project-remove-pycache-folders-and-pyc-files) question seems to be similar, but there is a difference: I would like my script to remove the folders itself in the end, not manually. – maxischl Sep 02 '20 at 20:04

6 Answers6

14

This will remove all *.pyc files and pycache directories recursively in the current directory:

  • with python:
import os

os.popen('find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf')
  • or manually with terminal or cmd:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
6

Bit of a frame challenge here: If you don't want the bytecode caches, the best solution is to not generate them in the first place. If you always delete them after every run, they're worse than useless. Either:

  1. Invoke python/python3 with the -B option (affects that single launch), or...
  2. Set the PYTHONDONTWRITEBYTECODE environment variable to affect all Python launches until it's unset, e.g. in bash, export PYTHONDONTWRITEBYTECODE=1

This does need to be set before the Python script is launched, so perhaps wrap your script with a simple bash script or the like that invokes the real Python script with the appropriate switch/environment set up.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • thanks for the suggestion, but that is not an option. I'll send the simulator to someone else and I don't want that person to do any big changes – maxischl Sep 02 '20 at 20:39
  • 1
    @maxischl: That's why I suggest the wrapper script; a three line (or less) wrapper script can remove the need for manual tweaks. – ShadowRanger Sep 02 '20 at 20:40
  • I think what you are suggesting is the proper and solid solution, but in this case I'll rather stick to the quick and dirty version – maxischl Sep 02 '20 at 20:42
3

Another simple solution is available if you have access to a Command-Line Interface and the find utility:

find . -type d -name __pycache__

as you will ask in plain language, it finds in the current folder (.) directories (-type d) that exactly match your pattern -name __pycache__. You can use this to identify where these folders are and then to delete them:

find . -type d -name __pycache__ -exec rm -fr {} \;

the huge advantage of this solution is that it transfers to other tasks easily (finding *.pyc files?) and has become an everyday tool for me.

meduz
  • 3,903
  • 1
  • 28
  • 40
2

Here is simple solution if you already know where the __pycache__ folders are just try the following

import shutil
import os

def clearCache():
    """
    Removes generic `__pycache__` .

    The `__pycache__` files are automatically created by python during the simulation.
    This function removes the genric files on simulation start and simulation end.
    """
    path = 'C:/Users/Yours/Desktop/LPG'
    try:
       for all in os.listdir(path):
        if os.path.isdir(path + all):
            if all == '__pycache__':
                shutil.rmtree(path + all, ignore_errors=False) 
    except:
        pass
clearCache()

Just simple you can still modify the path to the actually your path is. And if you want the script to penetrate into the subdirectories to remove the pycache folders just check the following

Example

import shutil
import os

path = 'C:/Users/Yours/Desktop/LPG'
for directories, subfolder, files in os.walk(path):
    if os.path.isdir(directories):
        if directories[::-1][:11][::-1] == '__pycache__':
                        shutil.rmtree(directories)

Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
0

If you want to delete any folders from any directory, use this function.
By default, it would start deleting from current directory and recursively goes in every sub-directories

import os
import shutil


def remove_dirs(curr_dir='./', del_dirs=['temp_folder', '__pycache__']):
    for del_dir in del_dirs:
        if del_dir in os.listdir(curr_dir):
            shutil.rmtree(os.path.join(curr_dir, del_dir))

    for dir in os.listdir(curr_dir):
        dir = os.path.join(curr_dir, dir)
        if os.path.isdir(dir):
            self.remove_dirs(dir, del_dirs)
rish_hyun
  • 451
  • 1
  • 7
  • 13
-1

You can use os with glob like this:

import os, glob

in_dir = "/path/to/your/folder"

pattern = ['__pycache__']

for p in pattern:
    [os.remove(x) for x in glob.iglob(os.path.join(in_dir, "**", p), recursive=True)]
Artur Nowicki
  • 441
  • 3
  • 7
  • 1
    That results in a `PermissionError: [Errno 1] Operation not permitted: ...` – maxischl Sep 02 '20 at 20:50
  • In some specific moments we get some errors. Even though it work in some cases, sometimes it doesn't `IsADirectoryError: [Errno 21] Is a directory: '../game/gui/__pycache__'` – Lixt Jun 30 '22 at 13:04