蟒蛇尝试不工作

Karen Jiang
# Test your program with the following:
# Case 1: When the user inputs 84, the program should delete the file 84.txt
# Case 2: When the user inputs 84 (again), the program should print a File Not Found error message
# Case 3: When the user inputs 5, the program should delete the file 5.txt
#TODO: Your code goes here
def FileDel(x):
    path = "/home/nbuser/library/parent_dir/files_exercises"
    ls = os.listdir(path)
    for i in ls:
        if x==i:
            os.remove(i)
            print(x,"removed")
        elif x.isdigit():
            x1=x+".txt"
            if x1==i:
                os.remove(i)
                print(x1,"removed")
        else:
            try:
                os.remove(i)
            except FileNotFoundError as exception_object:
                print("Cannot find file: ", exception_object)
            except PermissionError as exception_object:
                print("Cannot delete a directory: ", exception_object)
            except Exception as exception_object:
                print("Unexpected exception: ", exception_object)
                c_path = os.path.join(path, i)              


x=input("enter a file no. that you want to del,e.g. 5 or 5.txt: ")                    
FileDel(x)

enter a file no. that you want to del,e.g. 5 or 5.txt: 88
88.txt removed
Unexpected exception:  [Errno 21] Is a directory: 'dir_1'

上面的说明要求首先删除文件,然后第二次删除相同文件时,返回未找到的文件。但是,我的代码第一次将所有内容一起打印,对于第二次删除,它将打印:

enter a file no. that you want to del,e.g. 5 or 5.txt: 88
Unexpected exception:  [Errno 21] Is a directory: 'dir_1'

请帮忙!

我将以稍微不同的方式编写代码:

def FileDel(x):
    path = "/home/nbuser/library/parent_dir/files_exercises"
    ls = os.listdir(path)
    try:
        if str(x).isdigit():
            os.remove(path + '\\' + str(x) + ".txt")
            print(x, "removed")
        else:
            os.remove(path + '\\' + str(x) )
            print(x, "removed")

    except OSError as exception_object:
        print("Cannot find file: ", exception_object)
    except PermissionError as exception_object:
        print("Cannot delete a directory: ", exception_object)
    except Exception as exception_object:
        print("Unexpected exception: ", exception_object)
        c_path = os.path.join(path, i)


x=raw_input("enter a file no. that you want to del,e.g. 5 or 5.txt: ")
FileDel(x)

无论如何os.path.exists(path)您都可以使用它来提前告诉您路径/文件是否存在

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

蟒蛇飞盘。不工作 wxPython

为什么我的尝试,除了不能正常工作?蟒蛇 3

如果情况不工作与SQL查询蟒蛇空结果

尝试使用 ajax 上传(不工作)

为什么不尝试{} catch {}在C#中工作?

尝试用 String 和 int 从对象填充 ArrayList。不工作

关闭不传递值。蟒蛇3

蟒蛇。while循环中的变量不更新。

蟒蛇,时间数据..不匹配..错误

sorted() 列表未按预期工作。蟒蛇 2.7

取消发布发布者并尝试在不工作后立即发布

在这种情况下,为什么不尝试并抓住工作呢?

当我尝试刷新错误消息时,为什么不延迟工作?

18.04:即使尝试推荐的答案,ThinkPad p50 指纹扫描仪也不工作

unity OnTriggerEnter2d() 不工作 | 尝试在场景之间切换

C 编程 - Scanf 不工作。尝试添加空间,不起作用

尝试使用 coremltools 4.1 将模型转换为 coreml 不工作

Unity 2D OnTriggerEnter 不工作,尝试了一切

尝试在 main 方法中创建对象,但出现错误。蟒蛇 3

尝试从JSON中提取必填字段。(蟒蛇)

节点蟒蛇不返回相同的hash256

为什么不获取输入值呢?“蟒蛇”

蟒蛇:拆分字符串而不丢弃任何东西

为什么我的else语句不运行?[蟒蛇]

不能让webdriver.click()或.submit()的循环(蟒蛇)工作

蟒蛇海生。无法解释输入“工作日名称”

蟒蛇 3.6.5。为什么 OR 不像我期望的那样工作?

等待而不尝试捕获

需要工作但导入不工作