Forums

deleting a directory content using python script

hey I'm wondering how I can delete certain directory content using python script in pythonanywhere. I used to do it on my system with the following code:

def clean_folder(msg_id=None):
      files = glob.glob('/home/pedich/media/*')
      if msg_id == None:
            for f in files:
                os.remove(f)
      else:
           for f in files:
               if str(msg_id) in f:
                   os.remove(f)
      return None

but that doesnt work here! why is it such problem and how can I fix it?

What errors do you get?

thank you for your reply I don't get any errors it simply doesn't delete the files and they take up space so fast since its a bot which edits and processes videos so it should download them first. I checked the path its a corrects path and everything seems fine but it doesn't work unfortunately.

If there are no errors, maybe add some logging to the script, to see if it's actually doing what you think it's doing (e.g. that it's entering the conditial branches and executing os.remove parts).

thank you so much its fixed now.

Glad to hear that!