gpt4 book ai didi

python - 从文件名中去除路径

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:11 24 4
gpt4 key购买 nike

下面的代码块有效,但我想取消注释 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)

最佳答案

除非我完全遗漏了什么,否则你为什么不能做这样的事情?

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

关于python - 从文件名中去除路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com