我必须阅读 xlsx 格式的谷歌存储桶文件。桶中的文件结构看起来像
bucket_name
folder_name_1
file_name_1
folder_name_2
folder_name_3
file_name_3
python片段看起来像
def main():
storage_client = storage.Client.from_service_account_json(
Constants.GCP_CRENDENTIALS)
bucket = storage_client.bucket(Constants.GCP_BUCKET_NAME)
blob = bucket.blob(folder_name_2 + '/' + Constants.GCP_FILE_NAME)
data_bytes = blob.download_as_bytes()
df = pd.read_excel(data_bytes, engine='openpyxl')
print(df)
def function1():
print("no file in the folder") # sample error
在上面的代码片段中,我试图打开folder_name_2
,它返回一个错误,因为没有要读取的文件。
function1
当任何文件夹中没有文件时,我需要使用打印错误,而不是抛出错误。
有什么想法吗?