0

I am trying to write a program that automatically removes directories provided by the users input. However, when the code is executed I don't get a prompt asking me what directories I want to remove, therefore nothing actually gets removed or printed to the screen. Where am I going wrong? Am I missing something?

I have tried adding the 'input' function inside and outside the function, although I get the same output. The only outputting I keep getting is what is contained within the print function.

from sys import argv
import subprocess
import os

print ("""This tool is designed to remove multiple or single directories from your computer. \n You'll be asked the directory of which you wish to be removed.""")

name = argv(script[0])
directoryPath = input("Enter the directory to be deleted: ")

def removeDirectory(os):
    os.system("rm -rf", directoryPath)
    if os.stat(directoryPath) == 0:
        print (directoryPath, " has been successfully deleted")
    else:
        if os.stat(directoryPath) > 0:
            print ("The directory has been removed. Try re-running the script.")

My aim is to prompt the user (me) for the directory I want to be removed, then if successful, print the message '(directory) has been successfully deleted.'

Zoe
  • 27,060
  • 21
  • 118
  • 148
j0hn_n4te
  • 27
  • 5
  • 1
    One suggestion not related to the problem. There is no need to call `os.system("rm -rf", directoryPath)`. Use [`os.removedirs`](https://docs.python.org/3/library/os.html#os.removedirs) instead. – Matthias May 17 '19 at 18:41
  • Possible duplicate of [How to step through Python code to help debug issues?](https://stackoverflow.com/q/4929251/608639) – jww May 17 '19 at 18:51
  • Thanks for the suggestion. After, I changed the code from 'os.system("rm -rf")' to 'os.removedirs'. I got the following error: Enter the directory to be deleted: /root/copy0 Traceback (most recent call last): File "remove.py", line 20, in removeDirectory(directoryPath) File "remove.py", line 11, in removeDirectory os.removedirs(directoryPath) File "/usr/lib/python3.7/os.py", line 239, in removedirs rmdir(name) OSError: [Errno 39] Directory not empty: '/root/copy0' – j0hn_n4te May 17 '19 at 19:56

3 Answers3

2

I think you have forgotten to call the function that you have defined. Here's the same code with a new line:

from sys import argv
import subprocess
import os

# Function definition must happen before the function is called
def removeDirectory(directoryPath):
    os.system("rm -rf", directoryPath)
    if os.stat(directoryPath) == 0:
        print (directoryPath, " has been successfully deleted")
    else:
        if os.stat(directoryPath) > 0:
            print ("The directory has been removed. Try re-running the script.")

print ("""This tool is designed to remove multiple or single directories from your computer. \n You'll be asked the directory of which you wish to be removed.""")

name = argv(script[0])
directoryPath = input("Enter the directory to be deleted: ")
removeDirectory(directoryPath)      # < added this line

EDIT: as someone else pointed out, you should not use "os" as a parameter to your function (since it is already being used to reference the library you imported). I've changed that in the code above.

Owen
  • 404
  • 5
  • 13
  • Yes, of course, you must call the function :). In any case, I suggest having a main function called "main", then call and perform all your actions above inside it, and make sure you have the following configured: https://docs.python.org/3/library/__main__.html – Shani C May 17 '19 at 18:40
  • Thank you for the suggestion. After adding that line of code, I got the following error: Traceback (most recent call last): File "remove.py", line 11, in removeDirectory(directoryPath) NameError: name 'removeDirectory' is not defined – j0hn_n4te May 17 '19 at 18:42
  • @j0hn_n4te, Sorry, I forgot that function definitions must come before the function call. I updated the code. – Owen May 17 '19 at 18:51
1
from sys import argv
import subprocess
import os

def removeDirectory(directoryPath):
    os.system("rm -rfi {0}".format(directoryPath))
    if os.stat(directoryPath) == 0:
        print(directoryPath, " has been successfully deleted")
    else:
        if os.stat(directoryPath) > 0:
            print("The directory has been removed. Try re-running the script.")

def main():
    print("""This tool is designed to remove multiple or single directories from your computer. \n You'll be asked the directory of which you wish to be removed.""")

    name = argv(script[0])
    directoryPath = input("Enter the directory to be deleted: ")
    removeDirectory(directoryPath)

if __name__ == "__main__":
    # execute only if run as a script
    main()
Shani C
  • 166
  • 6
  • Thanks for the suggestion. However, after running that code, it gave me the following error: Traceback (most recent call last): File "remove.py", line 25, in main() File "remove.py", line 22, in main removeDirectory() File "remove.py", line 13, in removeDirectory os.system("rm -rf", directoryPath) TypeError: system() takes at most 1 argument (2 given) – j0hn_n4te May 17 '19 at 18:55
  • That is relevant to something else (I didn't go too much into your code, only gave you the basic guidelines I saw were missing), calling os.system with 2 parameters, is not good, you need to do this way (I did not check, but as I recall, this should work for you): ```os.system("rm -rf {0}".format(directoryPath))``` for example. I have updated the edit inside the code above. Another important note - it kind of scared me when you're executing ```rm -rf ``` on a given directory without even verifying with the user. I would suggest you ask user before deletion, by using ```rm -rfi ``` – Shani C May 17 '19 at 18:56
  • After many hours of trial and error. From one error, to another, I'm left with this error: Traceback (most recent call last): File "remove.py", line 25, in main(directoryPath) NameError: name 'directoryPath' is not defined Despite the fact that I have defined it, both inside and outside of functions. I keep getting the error that 'directoryPath, has not been defined' – j0hn_n4te May 17 '19 at 21:17
1

I just want to personally thank everyone that tried to help me with my problem. However, I have managed to find a solution. Instead, of using the function 'os.removedirs'. I used a function called 'shutil.rmtree(directoryPath)', which removed the inputted directory without warnings. Although, I couldn't have done this without the help I have received, so thank you to everyone that got involved!

j0hn_n4te
  • 27
  • 5