How to check existence of a folder with python and then remove it?
Asked Answered
R

5

83

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")
Reynaldoreynard answered 3/5, 2017 at 16:8 Comment(7)
@HFBrowning - that's not too effective against directories.Podagra
You don't use the files filename and even if you did, you need to add the original path ('dataset3') to it.Podagra
Then how I can do thatReynaldoreynard
By dataset folder, its literally named "dataset"?Podagra
If you know the name of the directory, there is no need for any checks. os.system("rm -rf dataset3/dataset") does the job.Podagra
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?Gorget
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 dataset3Reynaldoreynard
O
135

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)
Ostiary answered 3/5, 2017 at 23:40 Comment(3)
Is Path a relative path? I prefer absolute paths as is common sense.Pohl
@Timo: In this case its value happens to be relative. A 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.Ostiary
in both ancient and modern code snippets, isn't exists() and is_dir() the same as just is_dir() ?Connolly
C
30

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.

Crossover answered 22/9, 2020 at 16:26 Comment(1)
Yeah, so better not to do this, really :) - Otherwise you won't know if it failed to clear the folder.Developer
B
27

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")
Bigod answered 3/5, 2017 at 16:29 Comment(0)
G
1

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
Gorget answered 3/5, 2017 at 16:34 Comment(0)
R
0

This will do it:

for files in os.listdir('dataset3'):
     if files == 'dataset':
         os.rmdir(os.path.join(os.getcwd() + 'dataset3', files))
Rios answered 3/5, 2017 at 16:29 Comment(2)
For os.path.join(), you don't need/want to include the leading slash in the '/dataset3'.Ostiary
@Ostiary fixed!Rios

© 2022 - 2024 — McMap. All rights reserved.