从文件名剥离路径

时间:2011-03-21 03:20:57

标签: python scripting

下面的代码块有效,但我想取消注释 filename = os.path.basename(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 个答案:

答案 0 :(得分:6)

除非我完全遗漏某些东西,为什么你不能做这样的事情?

# 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)