Python: Difference between os.remove() and os.unlink() and which one to use?
Asked Answered
E

2

99

I have a number of files in a folder. I want to delete each file once it has been processed.

What's the difference between using os.remove() and os.unlink? Which method is ideal for my scenario?

Emogeneemollient answered 6/3, 2017 at 21:36 Comment(3)
@TadhgMcDonald-Jensen there are plenty of answers here on this site that are easily looked up in the documentation, it doesn't make them illegitimate. I myself am curious on why there are two different functions that are identical, usually Python is cleaner than that. It's certainly unexpected.Merbromin
@MarkRansom I agree, the why is actually the interesting question. Maybe there's some legacy reason. It's made more confusing because unix rm and unlink are different serverfault.com/questions/38816/…Spoken
@MarkRansom never implied this question is illegitimate, just that it's the kind of thing that is easily answered by using the docs, also redundancy for convinience isn't unheard of in python, just look at exit and quit.Nazler
F
96

Note: When this question was originally asked, it had a python-2.7 tag, which has since been removed. See the comments of this answer for discussion on the changes made in Python 3.


They are identical as described in the Python 2.7 documentation:

os.remove(path):

Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

Availability: Unix, Windows.

os.unlink(path):

Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name.

Availability: Unix, Windows.

Fredenburg answered 6/3, 2017 at 21:39 Comment(5)
Why do they both exist then? Is "unlink" so idiomatic to warrant a completely redundant function?Rancher
@DavidSkarbrevik Well.. WIndows people are used to remove(), unix people are used to unlink.. but also.. unlink() implies the unix filesystem behavior. When you unlink a file in unix, you aren't necessarily deleting it.. only deleting the specific hard link. If that hard link happens to be the only (or last remaining) link, then yes the file will cease to exist. With FAT/FAT32, when you delete a file it's gone, not just the link. With NTFS it's more sophisticated but still different. But basically unlink() is a unix thing and implies the unix behavior which isn't the same as Windows.Eardrum
Curiously, the Python 3 docs now describe the two functions as "semantically identical" (rather than simply "identical" or "the same function"). I also notice that os.remove is os.unlink is False, so they're not just distinct names for the same function object (though may have identical implementations). I wonder what's going on under the hood...Syllabism
@MarkAmery the reason the python 3 docs describe it that way is because I raised a bug report :) -- they used to be identical (read: os.unlink is os.remove) but after they were redone with argument clinic they are now two separate functions -- bugs.python.org/issue25930Nimwegen
In short, in python 2.7 they are both the same, only because the windows users are more familiar with remove() However, in python 3 they are two different functions. Thanks all it was a very helpful discussion.Phina
T
31

When using pathlib.Path file access in Python v3.4 and higher

While the question specifically asks for the os module file removal, the latest versions of Python have another option for removing files that may be an alternative.

Direct Answer - use pathlib.Path.unlink()

  • Note: pathlib.Path.remove() does not exist

When using the pathlib module for file access, use pathlib.Path.unlink() to remove files.

The Path.unlink() method is a replacement for both os.remove() and os.unlink(). It is executed directly on a Path object, rather than being passed the location of a file through a string argument.

More details

Starting in Python v3.4 the pathlib builtin module is available to handle file access in an object-oriented manner. I believe a separate package is also available via Pip for older versions of Python.

With pathlib, you create folder and file objects that are of the Path class. The related method of removing a file has been consolidated to just unlink(). They do not have a remove() method (likely because, per shash678's answer, there is no difference, it's just an alias). This appears to be equivalent to the os methods of file deletion, other than the underlying means of specifying the file itself.

See Object Oriented file system paths, along with the table at the bottom that shows both os.remove() and os.unlink() map to Path.unlink().

In Python v3.8, a missing_ok argument was added to the Path.unlink() function. When *missing_ok* == True, an exception will not be raised if the file doesn't exist before trying to remove it.

Teakwood answered 6/2, 2020 at 0:38 Comment(2)
Nice answer. But is not the answer to OP's question. That's why you have to SHOUT in the first line.Gerhan
@Gerhan Agreed that the answer is ancillary, which is why a header is all the more useful to readers, so they can quickly decide whether this answer addresses aspects of the question they are interested in or not. I feel most answers longer than a few lines would benefit from headers and other formatting flare.Teakwood

© 2022 - 2024 — McMap. All rights reserved.