Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django-storages delete folder

from django.core.files.storage import get_storage_class

default_storage = get_storage_class()()

@receiver(pre_delete, sender=OrganizationFile)
def delete_has_folder(sender, instance, *args, **kwargs):
    # get filename that will be equals to the relative path but not actually the filename
    path = Path(instance.file.name)
    # get a parent folder str
    folder_path = str(path.parent)
    
    # delete a file
    instance.file.delete()
    # delete a folder. 
    #  shutil.rmtree(absolute_path) wouldn't work
    #  because instance.file.path will raise an error.
    #  hence the only way is to delete with a storage default_storage.delete
    default_storage.delete(folder_path)

    logger.info(f'Pre delete {instance}. Deleted the hash folder {folder_path}')
Comment

PREVIOUS NEXT
Code Example
Python :: python take input without displaying it 
Python :: def create(self validated_data) 
Python :: install requests-html with conda 
Python :: numpy filter based on value 
Python :: 151 problem solution 
Python :: def total_missing(df,column_name) 
Python :: python mongodb docker 
Python :: python list sum 
Python :: length of dictionary python 
Python :: how to install pywhatkit in pycharm 
Python :: how to set class attributes with kwargs python 
Python :: if syntax in python 
Python :: python get function docstring 
Python :: avoid bad request django 
Python :: sqlalchemy one to one foreign key 
Python :: python json nan 
Python :: with open python print file name 
Python :: black jack python 
Python :: insert a new row to numpy array in especific position 
Python :: states and capitals us comma separated list 
Python :: making your own range function with step in python 
Python :: python get ids from array of objects 
Python :: how to find min, max in dictionaries 
Python :: sleep your computer python 
Python :: na.fill pyspark 
Python :: create a date value array in python 
Python :: how to round a number up in python 
Python :: ImportError: sys.meta_path is None, Python is likely shutting down 
Python :: What are Augmented Assignment Operators in python 
Python :: multiprocessing while applying a function in pandas 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =