I made this program to automate/simplify the erasing of a lot of files, I have some ideas to improve it but that's not why I'm putting it here, it's because it's a very dangerous program(it deletes stuff permanently) and since I'm a beginner(ish) I want some advice from better programmers to improve it.
import os
import send2trash
import shutil
file = input("Type the file path")
while os.path.exists(file) is False: # validation
file = input("Path not found, try again:")
for i, j, k in os.walk(file): # files check
print("The folder is in this path:", i)
print("The subfolders are named:", j)
print("The files are named:", k)
while True:
try:
command = int(input("1 - Delete permanently the file\n2 - Send the file to the bin"))
except ValueError:
print("Just the numbers 1 or 2 are accepted, follow the instructions")
continue
if command not in range(1, 3):
print("Just the numbers 1 or 2 are accepted, follow the instructions")
continue
else:
break
if command == 1:
try:
os.rmdir(file) # deletes if there is no inside folder
except OSError:
shutil.rmtree(file) # deletes if there is inside folders
else:
send2trash.send2trash(file) # send to the bin
2 Answers 2
There are some things that can be improved here
Divide your code into functions, don't put everything in the global namespace!
Use a
if __name__ == '__main__'
guard.It's not bad to give your code a little more space in the sake of readability, a few blank lines would help make this code look better.
Read PEP8#blank-lines for more details.
Don't use
==
oris
forFalse
,True
comparisonSee PEP8#programming-recommendations.
while os.path.exists(file) is False:
You can rewrite to a more idomatic
while not os.path.exists(file):
Naming is important
file = input("Type the file path")
This is a path, so rename it to
path = ...
for i, j, k in os.walk(file):
You could name those to what they represent:
for folder, subfolder, files in os.walk(path):
-
\$\begingroup\$ thanks for the readability tips, I suppose there is no danger or inefficiency in the code yet right? \$\endgroup\$Mateus– Mateus2017年12月21日 09:41:36 +00:00Commented Dec 21, 2017 at 9:41
-
1\$\begingroup\$ @Mateus I have not spotted something major yet, apart from putting everything in the global namespace, this looks pretty OK. \$\endgroup\$Ludisposed– Ludisposed2017年12月21日 10:04:03 +00:00Commented Dec 21, 2017 at 10:04
Unless this is just a homework or learning problem, I would do the following in addition to the accepted answer.
I would use command line args instead of prompts. This will make it potentially usefull as an automation tool for you later , look at the begins library, it makes this trivial.
I would absolutely add checks for critical file paths. It is highly unlikely this tool will be used to delete kernel files or program files (windows),, so it shouldn't be allowed to do so unless explicitly requested.
Like #2, deleting hidden files should be an explicit request.
You should create a history file of what has been deleted, so as last resort the user knows what just happened.
The program shouldn't be able to delete itself.
It should be possible to turn off delete confirmation, in case the tool is to be used in automation.
If you really want to be safe, implementing an undo will save your bacon at least once.
Given the potential of these types of applications to brick a user's OS, it should really be treated with extreme care.
0
- currently my only option is to kill the program. \$\endgroup\$Ctrl-C
will raise aKeyboardInterrupt
. \$\endgroup\$