[ Azure blob: upload directory content using a loop in Python ]
I would like to use a loop for uploading some files to a blob container. i.e.files xaa,xab,xac
I have tried the following loop but no success
import string
for i in string.lowercase[0:2]:
block_blob_service.create_blob_from_path(
'my_container',
'xa%s' % i,
'/pathtomylocalfile/xa%s' % i)
while this works
block_blob_service.create_blob_from_path(
'my_container',
'xaa',
'/pathtomylocalfile/xaa')
Answer 1
Otherwise, you can try to use format
function to format your string:
...
block_blob_service.create_blob_from_path(
'my_container',
'xa{}'.format(i),
'/pathtomylocalfile/xa{}'.format(i))
Answer 2
Strange that seems to work as an alternative
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir('/mylocaldirectory/') if isfile(join('/mylocaldirectory/', f))]
for i in onlyfiles:
block_blob_service.create_blob_from_path(
'mycontainer',
'%s' % i,
'/mylocaldirectory/%s' % i)