从文件名中删除路径

5
下面的代码块是可用的,但我想取消注释 filename = os.path.basename(filename),但如果这么做,我就不能指定 filename 的绝对路径,因为 k.set_contents_from_filename 将不再引用文件的实际位置,只有在当前工作目录下的文件才能生效。如果我不使用 filename = os.path.basename(filename),那么文件将被上传时带有它们的路径前缀。 有什么想法吗?
# 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)
1个回答

8
除非我完全错了,否则为什么你不能做这样的事情?
# 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接