6

Following this answer, I'm trying to delete the contents of a folder with this code

import os
import glob

files = glob.glob('/YOUR/PATH/*')
for f in files:
    os.remove(f)

But python returns an Attribution Error "'str' object has no attribute 'remove'". What am I'm doing wrong?

Community
  • 1
  • 1
Ricardo Dahis
  • 167
  • 1
  • 1
  • 8

2 Answers2

11

For deleting an entire directory, use shutil.rmtree('/your/path')

Read more from Python docs

Check out a similar question that has already been answered

Community
  • 1
  • 1
Bob Ezuba
  • 510
  • 1
  • 5
  • 22
  • 5
    This also deletes the enclosing folder, while I just want to delete the contents inside of it. – Ricardo Dahis Jan 25 '16 at 00:29
  • 3
    @RicardoDahis You can always recreate the empty directory once everything is removed. You dont have to loop through the individual files – karthikr Jan 25 '16 at 00:33
  • Thanks Bob! I'm doing this inside a loop, like `for folder in ['/folder1', '/folder2']: shutil.rmtree(path+folder) os.mkdir(path+folder)` But `os.mkdir` returns "'str' object has no attribute 'mkdir'". – Ricardo Dahis Jan 25 '16 at 01:02
  • @RicardoDahis You have corrupted your `os` import. You really do appear to have assigned a string to `os`. How about posting the full traceback? – mhawke Jan 25 '16 at 01:05
  • Am suspecting that something is wrong with the path you are supplying. Try os.mkdir(os.path.join(path, folder)) – Bob Ezuba Jan 25 '16 at 01:05
  • Sorry Bob, that also doesn't work. – Ricardo Dahis Jan 25 '16 at 01:07
  • Here's the full traceback: `Traceback (most recent call last): File "/Users/--/run_directory.py", line 19, in os.mkdir(path_build+folder) AttributeError: 'str' object has no attribute 'mkdir'` – Ricardo Dahis Jan 25 '16 at 01:09
  • @RicardoDahis: you can try all the `os` functions you like, however, _none_ of them will work whilst `os` is a reference to a string, and not the actual module. – mhawke Jan 25 '16 at 01:09
  • @mhawke, you are right! I was doing something really stupid here. Sorry for bothering and thank you all for the answers. – Ricardo Dahis Jan 25 '16 at 01:13
  • @RicardoDahis: thought so :) What was the problem? – mhawke Jan 25 '16 at 01:14
  • I was defining a variable called `os` some lines above for other purposes =O – Ricardo Dahis Jan 25 '16 at 01:26
0

As Bob Ezuba said in his answer, shutil.rmtree() is a better way to do it. You can recreate the directory if needed.

Using glob.glob('/your/path/*') will not find hidden files named with a leading .. You could call glob() multiple times, but that's getting ugly. Nor will glob() allow you to differentiate between files and directories, making it difficult to remove subdirectories. shutil.rmtree() will remove all files and subdirectories.

Alternatively you can rename the directory, recreate it anew, then rmtree() the old one. This might be better if you have any processes writing to files in the directory. And it won't leave your directory in a mess if rmtree() fails to remove some of the files, e.g. due to permissions.

Community
  • 1
  • 1
mhawke
  • 84,695
  • 9
  • 117
  • 138