Skip to content

Instantly share code, notes, and snippets.

@GreatBahram
Created November 24, 2019 06:09
Show Gist options
  • Save GreatBahram/a32491bb419db65af76e097b1e015849 to your computer and use it in GitHub Desktop.
Save GreatBahram/a32491bb419db65af76e097b1e015849 to your computer and use it in GitHub Desktop.
Useful context managers
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