从文件名剥离路径

Ast*_*ron 3 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)
Run Code Online (Sandbox Code Playgroud)

Mah*_*der 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)
Run Code Online (Sandbox Code Playgroud)