简体   繁体   English

Python程序未按预期提示用户

[英]Python program does not prompt user as expected

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. 我尝试在函数的内部和外部添加“ input”函数,尽管得到的输出相同。 The only outputting I keep getting is what is contained within the print function. 我唯一得到的输出是print函数中包含的内容。

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.' 我的目的是提示用户(我)要删除的目录,如果成功,则打印消息“(目录)已成功删除”。

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). 编辑:正如其他人指出的那样,您不应使用“ os”作为函数的参数(因为它已经被用来引用您导入的库了)。 I've changed that in the code above. 我已经在上面的代码中进行了更改。

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()

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'. 而是使用功能'os.removedirs'。 I used a function called 'shutil.rmtree(directoryPath)', which removed the inputted directory without warnings. 我使用了一个名为“ shutil.rmtree(directoryPath)”的函数,该函数在没有警告的情况下删除了输入的目录。 Although, I couldn't have done this without the help I have received, so thank you to everyone that got involved! 尽管,如果没有我的帮助,我无法做到这一点,所以感谢所有参与其中的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM