deleter.py


# Written by Acheev Bhagat
import os
from os import path
from os import remove
from os import walk
from os.path import isdir
from os.path import isfile
from os.path import getsize
from os.path import join
import shutil
from shutil import rmtree

#Program still contains a few bugs.
#These bugs are located in part 2 of the directory deleter
nus = raw_input('What would you like to delete(file or directory)?:')
if nus == 'directory':
    a = raw_input("Input the directory you want to delete(Remember to give full path ex. C:\): ")
    b = os.path.getsize(a)
    listing = raw_input("Do want a listing of all the folders and files in the directory?(y/n)?: ")
    if listing == "y":
        for dirname, dirnames, filenames in os.walk(a):
            for subdirname in dirnames:
                print os.path.join(dirname, subdirname)
            for filename in filenames:
                print os.path.join(dirname, filename)
    deletedyn = raw_input("Are you sure you want to delete this directory(y/n)? :")
    if deletedyn == "y":
        if b == 0:
            shutil.rmtree(a)
            c = isdir(a)
            if c == False:
                print 'Your directory has been deleted.'

        #The part below is just a replacement for part 2 of the directory deleter until it is fixed.
        if b != 0:
            shutil.rmtree(a)
            c = isdir(a)
            if c == False:
                print 'Your directory has been deleted.'
    if deletedyn == "n":
        print "Your directory will not be deleted."
    #Part 2 of the directory deleter is still buggy and must be fixed
    #It does not delete directories as it is supposed to but it does work perfectly fine on files.
    #The entire part will be counted as a comment until it is fixed.
    '''
    if b != 0:
        d = raw_input('Your directory contains files, are you sure you want to delete it(yes or no)?')
        if d == 'yes':
            shutil.rmtree(a)
            e = isdir(a)
            if e == False:
            print 'Your directory has been deleted.'
        if d == 'no':
            print 'This program will not delete your directory.'
        
    '''
if nus == 'file':
    f = raw_input("Input the file you want to delete(Remember to give full path ex. C:\example.txt): ")
    g = os.path.getsize(f)
    deletefyn = raw_input("Are you sure you want to delete your file(y/n)?: ")
    if deletefyn == "y":
        if g == 0:
            os.remove(f)
            h = isfile(f)
            if h == False:
                print 'Your file has been deleted.'
    if deletefyn == "n":
        print "Your file will not be deleted."
    if g != 0:
        i = raw_input('Your file contains information, do you want to delete it(yes or no)?: ')
        if i == 'yes':
            os.remove(f)
            j = isfile(f)
            if j == False:
                print 'Your file has been deleted.'
        if i == 'no':
            print 'This program will not delete your file.'

This was one of the first Python files I wrote that actually performed actions on the file system. This is what Dir.py was based off of.