Skip to content

Instantly share code, notes, and snippets.

Last active November 22, 2022 22:14
Show Gist options
  • Save christophermanning/1474925 to your computer and use it in GitHub Desktop.
Save christophermanning/1474925 to your computer and use it in GitHub Desktop.
Bitbucket Download Directory

Python script to download/shallow clone the files of a directory at bitbucket. This is useful when you just want a copy of the files in a subdirectory of a repository without needing mercurial or having to download the entire repository.

#!/usr/bin/env python
import os
import sys
import urllib
from urlparse import urlparse
try : import json
except : import simplejson as json
def open_directory(API_PATH, username, repo_slug, path):
directory_url = "%s/%s/%s/%s" % (API_PATH, username, repo_slug, path)
json_data_url_handle = urllib.urlopen(directory_url)
if json_data_url_handle.code != 200:
print "url %s not found" % directory_url
json_directory = json.loads(
for directory in json_directory['directories']:
open_directory(API_PATH, username, repo_slug, path + "/" + directory)
for file in json_directory['files']:
except OSError:
print "downloading %s" % file['path']
urllib.urlretrieve("%s/%s/%s/raw/%s/%s" % (API_PATH, username, repo_slug, file['revision'], file['path']), file['path'])
if (len(sys.argv) != 2 or sys.argv[1].find("") != 0 or sys.argv[1].find("/src/") == -1):
print "usage: python"
print "find the url by going to the source tab of a repository and browse to the directory you want to download"
null, username, repo_slug, path = urlparse(sys.argv[1]).path.split("/", 3)
open_directory(API_PATH, username, repo_slug, path)
Copy link

col000r commented Sep 27, 2018

This is great, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment