76

I want to remove dataset folder from dataset3 folder. But the following code is not removing dataset. First I want to check if dataset already exist in dataset then remove dataset.
Can some one please point out my mistake in following code?

for files in os.listdir("dataset3"):
    if os.path.exists("dataset"):
        os.system("rm -rf "+"dataset")
kpierce8
  • 15,977
  • 2
  • 23
  • 25
sara
  • 1,567
  • 4
  • 13
  • 21
  • @HFBrowning - that's not too effective against directories. – tdelaney May 03 '17 at 16:13
  • You don't use the `files` filename and even if you did, you need to add the original path ('dataset3') to it. – tdelaney May 03 '17 at 16:15
  • Then how I can do that – sara May 03 '17 at 16:16
  • By dataset folder, its literally named "dataset"? – tdelaney May 03 '17 at 16:17
  • If you know the name of the directory, there is no need for any checks. `os.system("rm -rf dataset3/dataset")` does the job. – tdelaney May 03 '17 at 16:19
  • you are looping over all files under "dataset3" and for each file in "dataset3" you are deleting "dataset" - "dataset" has to be on the same directory-level as "dataset3"!? Does this make sense? – dede May 03 '17 at 16:20
  • In my actual code when i run my programe it generates a dataset folder. I shif it in dataset3 folder.But when next time I run the same programe using same parameter I'm unable to move dataset into dataset3 because it already exists there. That's why I want to check its existance that if it already exists then remove it so that I can shift a new dataset into dataset3 – sara May 03 '17 at 16:31

5 Answers5

120

Python's os.rmdir() only works on empty the directories, however shutil.rmtree() doesn't care (even if there are subdirectories) which makes it very similar to the Linux rm -rf command.

import os
import shutil

dirpath = os.path.join('dataset3', 'dataset')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
    shutil.rmtree(dirpath)

Modern approach

In Python 3.4+ you can do same thing using the pathlib module to make the code more object-oriented and readable:

from pathlib import Path
import shutil

dirpath = Path('dataset3') / 'dataset'
if dirpath.exists() and dirpath.is_dir():
    shutil.rmtree(dirpath)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Is `Path` a relative path? I prefer absolute paths as is common sense. – Timo Jul 03 '21 at 14:45
  • 1
    @Timo: In this case its value happens to be relative. A [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) is a subclass of the `PurePath` class that's defined in the `pathlib` module and are not absolute nor relative per se — they can be either. These classes make accessing components of path easy to access and allows operations on them to be done in an object-oriented fashion. Also note that sometime using a relative path makes perfect sense — such as when you don't want to hardcode an absolute path into your code or won't know what the root folder is until runtime. – martineau Jul 03 '21 at 15:27
  • in both ancient and modern code snippets, isn't `exists() and is_dir()` the same as just `is_dir()` ? – Don Hatch Sep 24 '22 at 06:00
24

os.remove() is to remove a file.

os.rmdir() is to remove an empty directory.

shutil.rmtree() is to delete a directory and all its contents.

import os

folder = "dataset3/"

# Method 1
for files in os.listdir(folder):
    if files == "dataset":
        os.remove(folder + "dataset")

# Method 2
if os.path.exists(folder + "dataset"):
    os.remove(folder + "dataset")
erip
  • 16,374
  • 11
  • 66
  • 121
Huang Yen Hao
  • 434
  • 2
  • 6
24

Better to set ignore_errors:

import shutil

shutil.rmtree('/folder_name', ignore_errors=True)

This is much more readable, and concise.

Note that it will ignore all errors, not just dir missing errors.

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
  • 5
    Yeah, so better not to do this, really :) - Otherwise you won't know if it failed to clear the folder. – O'Rooney Aug 05 '22 at 00:21
0

This will do it:

for files in os.listdir('dataset3'):
     if files == 'dataset':
         os.rmdir(os.path.join(os.getcwd() + 'dataset3', files))
lch
  • 2,028
  • 2
  • 25
  • 46
0

try this:

for files in os.listdir("dataset3"):
  if files=="dataset":
    fn=os.path.join("dataset3", files)
    os.system("rm -rf "+fn)
    break

You do not need the os.path.exists() because os.listdir() already told you, that it exists.

And if your foldernames are static, you can do it with:

if os.path.exists("dataset3/dataset"):
  os.system("rm -rf dataset3/dataset")

or as:

try:
  os.system("rm -rf dataset3/dataset")
except:
  pass
dede
  • 706
  • 9
  • 19