我找到了一种方法,可以一次将多个csv文件上载到满足特定条件的Google云存储桶中。我的问题是,当他们全部上载到google存储桶时,文件的整个路径名都随之上传了。我只想上传实际的文件名
我尝试使用os.path.basename,但是它不起作用。还有其他方法可以在上传之前仅获取基本名称,还是可以在上传之前简单地重命名文件?
import glob
import os
from pathlib import Path
from os import listdir
from google.cloud import storage
GOOGLE_APPLICATION_CREDENTIALS = "O:\My Creds\creds.json"
for file in glob.glob("O:\Team Drives\AU_A\Raw_Dauts\Dynamets\**\*.csv", recursive = True):
filename = os.path.basename(file) # throught this would work but doesn't
storage_client = storage.Client.from_service_account_json(GOOGLE_APPLICATION_CREDENTIALS)
bucket = storage_client.get_bucket('bukcetang81')
blob = bucket.blob("Dynamic_datasets/" +filename)
blob.upload_from_filename(filename)
答案 0 :(得分:0)
我建议您删除参数“文件名”,而不是使用文件本身的名称对其进行硬编码:
for file in glob.glob("O:\Team Drives\AU_A\Raw_Dauts\Dynamets\**\*.csv", recursive = True):
storage_client = storage.Client.from_service_account_json(GOOGLE_APPLICATION_CREDENTIALS)
bucket = storage_client.get_bucket('bukcetang81')
blob = bucket.blob(destination_filename)
blob.upload_from_filename(source_filename)