6

I have my code as follows -

#!/usr/bin/env python
import time, glob, os, sys
from datetime import date, timedelta

try:
    dpath = sys.argv[1]+"/"
except:
    print "usage: " + sys.argv[0] +" <dir_path_to_purge_files>"
    sys.exit(1)
print dpath
day_minus_mtime = time.mktime(date.today().timetuple())
g = glob.glob(dpath+"*")
for f in g:
        try:
                if day_minus_mtime > os.path.getmtime(f):
                        os.remove(f)
                        print "Removed: "+f
        except OSError, e:
                print "Not able to Remove: "+f , e

I believe that os.remove(file) is equivalent to "rm file" in linux.

I would like to know the equivalent function for "rm -f file". Forcefully remove a file or Forcefully unlink the file path from directory.

Also the above code is trying to purge files older than today. I have a situation where the files are not deleted as it is "write-protected" due to the ownership. But when I use "rm -f" to the same file; it is getting deleted.

I think it is better to ask a question, even though it sounds stupid to yourselves

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
aslamplr
  • 65
  • 1
  • 2
  • 7
  • 1
    For me, `os.remove()` even deletes files with all permission bits unset and belonging to a different user, i.e. `os.remove()` does `rm -f` for me. Please provide the full error message you get. – Sven Marnach Mar 04 '12 at 15:46
  • `os.remove`/`os.unlink` (both are the same) do basically the same. If the file is not deleted do to *ownership*, then you cannot delete it using `rm -f` either. If it's just a matter of permissions... then the only that matter are the permissions over the directory, not the file itself, as @SvenMarnach mentions. – Ricardo Cárdenes Mar 04 '12 at 15:51
  • I think you are right, this is something to do with the NAS storage mounted as NFS in the server where I'm running this. – aslamplr Mar 04 '12 at 15:51
  • @RicardoCárdenes: The ownership of the file also doesn't matter on Linux -- you can delete anybody's files as long as you have write permission on the directory. – Sven Marnach Mar 04 '12 at 15:55
  • `user@server:/path_to_scripts/Scripts$ /path_to_scripts/Scripts/purgedir.py /path_to_rep_temp/replicate/tmp /path_to_rep_temp/replicate/tmp Not able to Remove: /path_to_rep_temp/replicate/tmp/daf.fefl.20120304.2393.ddl [Errno 2] No such file or directory: '/path_to_rep_temp/replicate/tmp/daf.fefl.20120304095323.2393.20120304094623839.ddl' Not able to Remove: /path_to_rep_temp/replicate/tmp/daf.fefl.20120304.2393.ddl.noidx [Errno 2] No such file or directory: '/path_to_rep_temp/replicate/tmp/daf.fefl.20120304.2393.ddl.noidx'` – aslamplr Mar 04 '12 at 15:59
  • This seems to have problem with the NAS storage, the files listed by glob is not available after sometime. – aslamplr Mar 04 '12 at 16:00
  • Thank you @Seven and @Ricardo; I will contact the storage people to see if there is some issue with that. All my question was "is there any forceful remove function available in python. That is got answered – aslamplr Mar 04 '12 at 16:03

1 Answers1

5

The --force option to rm means, to ignore non existing files and never prompt, according to my man page.

The never prompt part is easy, your python remove does not prompt, right?

The ignore non existing files is also easy: you could either check, if the file exists, right before you remove it. You have a small race condition, because the file might disappear between the existence check and the remove. Or you could catch the OSError, and verify that it is thrown because the file does not exist (OSError: [Errno 2] No such file or directory...). One other reason for the OSError is, that the file you want to remove is not a file but a directory.

The force option does mo permission magic (at least on my linux), just keep in mind, that removing a file is a write operation on the directory.

Jörg Beyer
  • 3,631
  • 21
  • 35
  • To be picky, removing a file in a directory with its sticky bit set -such as `/tmp/` for instance- requires also ownership of the file itself (in addition of write permission on the directory). – Basile Starynkevitch Mar 04 '12 at 16:13
  • @JörgBeyer: I think, I don't have the reputation to Vote Up. – aslamplr Mar 04 '12 at 16:14
  • you could, if you like, accept the answer - without any reputation. That is something different then up voting, see http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jörg Beyer Mar 04 '12 at 16:18
  • For checking if the exception was because the file does not exist, it is better to check if `ex.errno == errno.ENOENT`, rather than rely on string matching. – itsadok Jul 04 '18 at 14:02