我试图编写一个程序,自动删除由用户输入提供的目录。但是,当代码执行时,我没有得到一个提示,询问我要删除哪些目录,因此实际上没有任何内容被删除或打印到屏幕上。我哪里做错了?我遗漏了什么吗?
我试过在函数内部和外部添加“input”函数,但得到的输出是相同的。我得到的唯一输出是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.")

我的目的是提示用户(我)要删除的目录,如果成功,则打印消息'(目录)已成功删除。'

最佳答案

我想你忘记调用你定义的函数了。这里有一个新行的相同代码:

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

编辑:正如其他人指出的,您不应该使用“os”作为函数的参数(因为它已经被用来引用您导入的库)。我已经在上面的代码中修改了。

关于python - Python程序未按预期提示用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56191792/

10-11 22:37
查看更多