I want to make sure that I delete required files.
I have code something like
dir="/some/path/"
file = "somefile.txt"
cmd_rm= "rm -rf "+dir + file
os.system(cmd_rm)
The dir and file values are fetched from a database. How can I make sure I never end up running rm -rf /?
What things should I check before doing rm -rf?
Don’t use the
-rswitch if you just want to remove a single file. Also, there could be spaces in the file name.Better use the functions in Python’s
osmodule instead:Normalizing the path with
abspathand comparing it against the target directory avoids file names like “../../../etc/passwd” or similar.