Welcome, guest | Sign In | My Account | Store | Cart

This small program lets a user delete a directory on their Windows system. Also good for learning from.

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import os
from os import path
from os.path import isdir
from os.path import getsize
import shutil
from shutil import rmtree

a = raw_input("Input the directory you want to delete(Remember to give full path ex. C:\): ")
b = os.path.getsize(a)
if b == 0:
    shutil.rmtree(a)
    c = isdir(a)
    if c == False:
        print 'Your directory has been deleted.'

You might want to use this to delete any directory on a Windows system. Not really as useful as cool to use and learn from. Works better on directories than files.

3 comments

Stephen Chappell 10 years, 5 months ago  # | flag

If you want to semi-securing delete a directory or file, you might want to check recipe 577670 for a more in-depth approach to the topic.

mJack (author) 10 years, 5 months ago  # | flag

Another side note is that this is better for Windows systems

Lighton Phiri 10 years, 2 months ago  # | flag

So I am fairly new to Python and I am wondering if the import statements belows are necessary, since you already have the main import statement in line #1

from os import path
from os.path import isdir
from os.path import getsize