Skip to content

Instantly share code, notes, and snippets.

@andrewsolomon
Last active March 1, 2021 14:14
Show Gist options
  • Save andrewsolomon/097c9dc716c9290144fe08b726547d88 to your computer and use it in GitHub Desktop.
Save andrewsolomon/097c9dc716c9290144fe08b726547d88 to your computer and use it in GitHub Desktop.
Optimised Python
# Standard Python
try:
os.remove('somefile.tmp')
except FileNotFoundError:
pass
# Improved Python
import contextlib
with contextlib.suppress(FileNotFoundError):
os.remove('somefile.tmp')
# Perl
unlink 'somefile.tmp';
# Python >= 3.4
import pathlib
pathlib.Path("somefile.tmp").unlink(missing_ok = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment