forked from inspirezonetech/TeachMePythonLikeIm5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleting-files.py
31 lines (24 loc) · 1.07 KB
/
deleting-files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# ------------------------------------------------------------------------------------
# Tutorial: Receives a file name from a user and deletes it
# ------------------------------------------------------------------------------------
import os
def delete_file(filename):
try:
os.remove(filename) # deletes filename
print(f'{filename} has been deleted') # prints out success message
except OSError:
print('No such file in directory!') # prints error message when file not found
def main():
filename = None
try:
# get the filename from the user
filename = input('Please enter name (with type) of the file you would like to remove: ')
except EOFError:
return filename
# call delete function from above and pass in the filename
delete_file(filename)
if __name__ == '__main__':
main()
# ------------------------------------------------------------------------------------
# Challenge: Delete a specific file in directory
# ------------------------------------------------------------------------------------