Python 从文件名中删除路径

Python 从文件名中删除路径,python,scripting,Python,Scripting,下面的代码块可以工作,但我想取消注释filename=os.path.basename(filename),这样做时,我无法为filename指定绝对路径,因为k.set\u contents\u from\u filename将不再引用文件的实际位置,如果未注释,则只有当前工作目录中的文件才能工作。如果我不使用filename=os.path.basename(filename),则将上载这些文件,并保留其路径。有什么想法吗 # List files in directory and uplo

下面的代码块可以工作,但我想取消注释filename=os.path.basename(filename),这样做时,我无法为filename指定绝对路径,因为k.set\u contents\u from\u 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)

除非我完全错过了什么,你为什么不能做这样的事

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