Skip to content

Instantly share code, notes, and snippets.

@anna-anisienia
Created September 29, 2020 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anna-anisienia/2f1be5e22bd492d3141acbf4a29c4cc6 to your computer and use it in GitHub Desktop.
Save anna-anisienia/2f1be5e22bd492d3141acbf4a29c4cc6 to your computer and use it in GitHub Desktop.
import os
import contextlib
import boto3
s3 = boto3.client('s3', aws_access_key_id='my_aws_access_key',
aws_secret_access_key='my_aws_secret_key',
region_name='eu-central-1')
@contextlib.contextmanager
def this_directory(path):
"""
Change the working dir to the path specified. Then, change back to the original one.
"""
original_workdir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(original_workdir)
# Usage:
with this_directory(path='../data'):
file = 'my_s3_file.csv'
s3.download_file(bucket='s3_bucket', key=file, filename=file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment