I want to remove some log files of my App server without shutting down my server. What command can I use to do this using Python, like rm -rf in Linux systems?
Please help.
I want to remove some log files of my App server without shutting down my server. What command can I use to do this using Python, like rm -rf in Linux systems?
Please help.
 
    
     
    
    shutil is your friend in this instance.
http://docs.python.org/2/library/shutil.html#shutil.rmtree
import shutil
shutil.rmtree("/my/path/to/folder/to/destroy")
 
    
    #!/usr/bin/env python             
import os
def nukedir(dir):
    if dir[-1] == os.sep: dir = dir[:-1]
    files = os.listdir(dir)
    for file in files:
        if file == '.' or file == '..': continue
        path = dir + os.sep + file
        if os.path.isdir(path):
            nukedir(path)
        else:
            os.unlink(path)
    os.rmdir(dir)
nukedir("/home/mb/test");
Above function will delete any directory recursively...
 
    
    Is your server running Linux, or is that just an example?
On python, shutil.rmtree() is the equivalent to rm -r (as @Alex already answered). All python removal commands (os.unlink(), os.rmdir()) work without checks, so they're always equivalent to rm -f.
But if you're on Windows, the OS will not let you delete a file that's still open; you'll get an exception. AFAIK there's nothing an unprivileged process can do about it.
 
    
    You can use the subprocess module:
from subprocess import Popen, PIPE, STDOUT
cmd = 'rm -frv /path/to/dir'
p   = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
out = p.stdout.read()
print out