3
# 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'

the instruction above asks to remove a file first, then when you remove the same file for the second time, returns files not found. however, my code prints everything together for the first time, for the second remove, it prints:

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'

please help!

BlueSheepToken
6,2073 gold badges24 silver badges47 bronze badges
asked Aug 23, 2018 at 7:35
3
  • 1
    Is your code properly indented? I see a for loop matched with an else statement. Commented Aug 23, 2018 at 7:50
  • 1
    For/else exists book.pythontips.com/en/latest/for_-_else.html Commented Aug 23, 2018 at 7:52
  • I see, you learn something new every day. Commented Aug 23, 2018 at 7:53

1 Answer 1

2

I would write the code in a bit slightly different way:

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)

You can use anyway os.path.exists(path) which tells you in advance if the path/file exists or not

answered Aug 23, 2018 at 7:59
Sign up to request clarification or add additional context in comments.

1 Comment

after I changed '\\' to '/', it works perfectly, thank you very much!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.