11

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.

dda
  • 6,030
  • 2
  • 25
  • 34
user1344022
  • 193
  • 2
  • 3
  • 10

4 Answers4

11

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")
Alex Couper
  • 910
  • 9
  • 17
  • 6
    Does anyone else see the irony in this smug response when the top result in the Google reference is this very question? – RussellStewart May 09 '14 at 23:08
  • 1
    While useful, `rmtree` isn't equivalent: it errors out if you try to remove a single file. See my answer here: http://stackoverflow.com/a/9559881/260491 – Gabriel Grant Oct 05 '15 at 13:42
7
#!/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...

RamneekHanda
  • 171
  • 5
  • HI thanks for the Reply but the problem is i want to delete the Weblogic log file by using WLST with Pythan. – user1344022 Dec 10 '12 at 13:05
  • This seems like it should work for directories, but it isn't equivalent to `rm -rf`: it errors out if you try to remove a single file. Also, it's best to use the well-tested code from Python's standard library when possible, rather than writing your own. See my four-line `rm -rf` function here: http://stackoverflow.com/a/9559881/260491 – Gabriel Grant Oct 05 '15 at 13:49
  • you don't need to write your own. Python's standard library already has shutil.rmtree() which recursively deletes – Corey Goldberg Jun 14 '17 at 06:23
5

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.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • HI alexis,My server is running on the linux machine. The Problem is i dont have permission to log in as Weblogic user. So I used to log in as the user who don't have the permission to delete the Weblogic log File. I am Using WLST with Pythan. IS there a way to delete Files BY using WLST. – user1344022 Dec 10 '12 at 13:02
  • No idea what WLST or Weblogic is. To delete a file, you must be able to run _some_ program under a process that has permission to delete it. If python's `os.unlink()` can't do it, there's nothing you can do in _any_ programming language: The OS won't let you. – alexis Dec 10 '12 at 15:50
  • While useful, `rmtree` isn't equivalent: it errors out if you try to remove a single file. See my answer here: http://stackoverflow.com/a/9559881/260491 – Gabriel Grant Oct 05 '15 at 13:43
1

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
  • I'm reviewing some code and the developer used subprocess rm vs unlink. They were using unlink at first and then switched to `subprocess.call(['rm', '-f', file_path])`. I'm just trying to understand why they would do that, and if there is an increase in performance? – radtek Apr 27 '17 at 19:58
  • Because none of the built in methods to delete files or folders is reliable, even with all the workarounds you might find. – omni Mar 26 '20 at 14:06
  • This could also work with the `os` module (built-in), and it works, but it's far away from "Pythonic code". – Andrew Aug 17 '23 at 10:58