gpt4 book ai didi

Python 程序没有按预期提示用户

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:42 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,自动删除用户输入提供的目录。但是,当执行代码时,我没有收到询问我要删除哪些目录的提示,因此实际上没有任何内容被删除或打印到屏幕上。我哪里错了?我错过了什么吗?

我尝试在函数内部和外部添加“输入”函数,但得到的输出相同。我不断得到的唯一输出是打印函数中包含的内容。

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 程序没有按预期提示用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56191792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com