views:

827

answers:

5

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?

+10  A: 

You might consider using os.remove() instead since it's a great deal less dangerous than what you're attempting.

inkedmn
+6  A: 

First, I suggest you to use the os.remove() and os.rmdir() functions for working with things like that. You will end up with more portable code and less headache for checking command return.

To check what you are effectively attempting to remove (you may not want to just check "/"), you can use some regular expressions on the generated path or just add a base path to all path returned from you database (depending what you are doing ...).

Kaltezar
If you want to check against the path with a regular expression, don't forget to do something like os.path.realpath and/or os.path.normpath to get a canonical and easy-to-deal-with path string (in particular, without os.path.realpath() (or equivalent code that simply can't be done with regexes), you have no way of knowing that "foo/bar" actually refers to "/")
Devin Jeanpierre
+7  A: 

Don't use the -r switch 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 os module instead:

dirname = "/some/path/"
filename = "somefile.txt"
pathname = os.path.abspath(os.path.join(dirname, filename))
if pathname.startswith(dirname):
   os.remove(pathname)

Normalizing the path with abspath and comparing it against the target directory avoids file names like "../../../etc/passwd" or similar.

sth
+2  A: 

There is a module called shutil that provides shell-like file manipulation. If you want to delete a directory and all files and directories in it then use shutil.rmtree.

However it is implemented in python so if you are deleting a huge number of files then spawning rm may be faster, but will fail if the path has a space in it.

Dave Kirby
I want to vote you up for the shutil suggestion ( http://docs.python.org/library/shutil.html , for a link), but at the same time spawning rm doesn't need to fail because of spaces etc. (use the subprocess module, man!), and speed is almost certainly not an issue (frankly, Python isn't that slow, and I'm fairly sure this op isn't usually CPU-bound).
Devin Jeanpierre
+1  A: 

Use shutil.rmtree as Dave Kirby says. If you want to delete the just the file use:

dir="/some/path/" 
file = "somefile.txt" 
cmd= os.path.join(dir, file) 
shutil.rmtree(cmd_rm) 

If you want to delete the directory use:

dir="/some/path/" 
file = "somefile.txt"  
shutil.rmtree(dir) 

If the files are write protected make sure you have write permissions before you run this.

chrissygormley