简体   繁体   中英

Strip path from filename

The code block below works but I would like to un-comment filename = os.path.basename(filename) , when I do I am unable to specify an absolute path for filename because k.set_contents_from_filename will not longer reference the the actual location of the file, only file in the current working directory will work if un-commented. If I do not use filename = os.path.basename(filename) then the file(s) will be uploaded with their path pre-pended. Any ideas?

# List files in directory and upload them to bucket
    for filename in all_files:
        #skip all directory entries which are not a file
        if not os.path.isfile(filename):
              continue
        #filename = os.path.basename(filename)           
        k = Key(bucket)
        k.key = filename
        k.set_contents_from_filename(filename, cb=percent_cb, num_cb=10)

Unless I'm missing something completely, why can't you do something like this?

# List files in directory and upload them to bucket
for filename in all_files:
    #skip all directory entries which are not a file
    if not os.path.isfile(filename):
          continue    
    k = Key(bucket)
    k.key = os.path.basename(filename)
    k.set_contents_from_filename(filename, cb=percent_cb, num_cb=10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM