open(sys.argv[3],'w').write('') with open(sys.argv[3], 'a') as outfile: for i in collectionName.find(): json.dump(i, outfile,default=json_util.default) outfile.write('\n') print 'backup completed' outfile = sys.argv[3] client = document_client.DocumentClient('https://XXXXXXXX.documents.azure.com:443', {'masterKey': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=='}) # Attempt to delete the database. This allows this to be used to recreate as well as create try: db = next((data for data in client.ReadDatabases() if data['id'] == db)) client.DeleteDatabase(db['_self']) print 'test database deleted' except: pass # Create database try: db = client.CreateDatabase({ 'id': db }) print 'test database created' except: pass # Create collection try: collection = client.CreateCollection(db['_self'],{ 'id': collectionName }) print 'restaurants collection created'