Last active
March 1, 2021 14:14
-
-
Save andrewsolomon/097c9dc716c9290144fe08b726547d88 to your computer and use it in GitHub Desktop.
Optimised Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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