Created
November 24, 2019 06:09
-
-
Save GreatBahram/a32491bb419db65af76e097b1e015849 to your computer and use it in GitHub Desktop.
Useful context managers
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
def tmp_copy(source, destination): | |
import shutil | |
import os | |
shutil.copy(source, destination) | |
try: | |
yield | |
finally: | |
os.remove(destination) | |
def chdir(directory): | |
from pathlib import Path | |
import os | |
cwd = Path.cwd() | |
os.chdir(directory) | |
try: | |
yield | |
finally: | |
os.chdir(cwd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment