如何制作os.删除不能在删除fi失败后关闭我的程序

2024-04-19 03:55:01 发布

您现在位置:Python中文网/ 问答频道 /正文

这是密码

    import os
    while True:
        print "Upisite ime datoteke koju zelite izbrisati"
        izbrisi = os.remove(raw_input(""))

因此,我运行这个程序试图删除文件,但该文件不存在,所以它只是关闭程序,即使它在while循环(当文件确实存在循环continiues)。 我试着用if语句,但没能成功。你知道吗


Tags: 文件importtrue密码osremoveprintwhile
1条回答
网友
1楼 · 发布于 2024-04-19 03:55:01

你可以试试:

import os
while True:
    print "Upisite ime datoteke koju zelite izbrisati"
    try:
        fname = raw_input("")
        izbrisi = os.remove(fname)
    except OSError as err:
        print "failed to remove %s" % fname
        pass #ignore the exception

相关问题 更多 >