Most pythonic way to delete a file which may not exist

Python

Python Problem Overview


I want to delete the file filename if it exists. Is it proper to say

if os.path.exists(filename):
    os.remove(filename)

Is there a better way? A one-line way?

Python Solutions


Solution 1 - Python

A more pythonic way would be:

try:
    os.remove(filename)
except OSError:
    pass

Although this takes even more lines and looks very ugly, it avoids the unnecessary call to os.path.exists() and follows the python convention of overusing exceptions.

It may be worthwhile to write a function to do this for you:

import os, errno

def silentremove(filename):
    try:
        os.remove(filename)
    except OSError as e: # this would be "except OSError, e:" before Python 2.6
        if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
            raise # re-raise exception if a different error occurred

Solution 2 - Python

I prefer to suppress an exception rather than checking for the file's existence, to avoid a TOCTTOU bug. Matt's answer is a good example of this, but we can simplify it slightly under Python 3, using contextlib.suppress():

import contextlib

with contextlib.suppress(FileNotFoundError):
    os.remove(filename)

If filename is a pathlib.Path object instead of a string, we can call its .unlink() method instead of using os.remove(). In my experience, Path objects are more useful than strings for filesystem manipulation.

Since everything in this answer is exclusive to Python 3, it provides yet another reason to upgrade.

Solution 3 - Python

As of Python 3.8, use missing_ok=True and pathlib.Path.unlink (docs here)

from pathlib import Path

my_file = Path("./dir1/dir2/file.txt")

# Python 3.8+
my_file.unlink(missing_ok=True)

# Python 3.7 and earlier
if my_file.exists():
    my_file.unlink()

Solution 4 - Python

os.path.exists returns True for folders as well as files. Consider using os.path.isfile to check for whether the file exists instead.

Solution 5 - Python

In the spirit of Andy Jones' answer, how about an authentic ternary operation:

os.remove(fn) if os.path.exists(fn) else None

Solution 6 - Python

if os.path.exists(filename): os.remove(filename)

is a one-liner.

Many of you may disagree - possibly for reasons like considering the proposed use of ternaries "ugly" - but this begs the question of whether we should listen to people used to ugly standards when they call something non-standard "ugly".

Solution 7 - Python

Another way to know if the file (or files) exists, and to remove it, is using the module glob.

from glob import glob
import os

for filename in glob("*.csv"):
    os.remove(filename)

Glob finds all the files that could select the pattern with a *nix wildcard, and loops the list.

Solution 8 - Python

Matt's answer is the right one for older Pythons and Kevin's the right answer for newer ones.

If you wish not to copy the function for silentremove, this functionality is exposed in path.py as remove_p:

from path import Path
Path(filename).remove_p()

Solution 9 - Python

In Python 3.4 or later version, the pythonic way would be:

import os
from contextlib import suppress

with suppress(OSError):
    os.remove(filename)

Solution 10 - Python

Something like this? Takes advantage of short-circuit evaluation. If the file does not exist, the whole conditional cannot be true, so python will not bother evaluation the second part.

os.path.exists("gogogo.php") and os.remove("gogogo.php")

Solution 11 - Python

A KISS offering:

def remove_if_exists(filename):
  if os.path.exists(filename):
    os.remove(filename)

And then:

remove_if_exists("my.file")

Solution 12 - Python

This is another solution:

if os.path.isfile(os.path.join(path, filename)):
	os.remove(os.path.join(path, filename))

Solution 13 - Python

This will do in one line as well as checks if the path exists, else nothing really happens

os.remove(my_path) if os.path.exists(my_path) else None

Solution 14 - Python

Since Python 3.3 you can use FileNotFoundError which is more correct than the accepted version since it doesn't ignore other possible errors.

try:
    os.remove(filename)
except OSErrorFileNotFoundError
    pass

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScott C WilsonView Question on Stackoverflow
Solution 1 - PythonMattView Answer on Stackoverflow
Solution 2 - PythonKevinView Answer on Stackoverflow
Solution 3 - PythonwkeithvanView Answer on Stackoverflow
Solution 4 - PythonaboughtView Answer on Stackoverflow
Solution 5 - PythonTim KeatingView Answer on Stackoverflow
Solution 6 - PythonDevonMcCView Answer on Stackoverflow
Solution 7 - PythonjotacorView Answer on Stackoverflow
Solution 8 - PythonJason R. CoombsView Answer on Stackoverflow
Solution 9 - PythonRoss CastroverdeView Answer on Stackoverflow
Solution 10 - PythonAndy JonesView Answer on Stackoverflow
Solution 11 - PythonBazView Answer on Stackoverflow
Solution 12 - PythonKianView Answer on Stackoverflow
Solution 13 - PythonRagsView Answer on Stackoverflow
Solution 14 - PythonPaulView Answer on Stackoverflow