python - WindowsPath 无法转换为 unicode

标签 python google-cloud-storage

我有以下 python 代码,它试图将文件上传到谷歌云存储桶,当我运行它时,我收到以下错误

 "WindowsPath('sample.flac') could not be converted to unicode"

这是我的代码

from pathlib import Path
from google.cloud import storage

filename = Path(sys.argv[1])
remote_filename = filename.with_suffix('.flac')


def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print('  File {} uploaded to {}.'.format(
        source_file_name,
        destination_blob_name))


upload_blob('speech-demo-2',filename,remote_filename)

我不明白它出了什么问题。 而且我的 Python 知识充其量也只是初级的。我一周前才开始学习Python。

编辑:我包括完整的回溯

Traceback (most recent call last):
  File "F:\cloud-speech-to-text\test.py", line 53, in <module>
    upload_blob('speech-demo-2',filename,remote_filename)
  File "F:\cloud-speech-to-text\test.py", line 21, in upload_blob
    blob = bucket.blob(destination_blob_name)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\bucket.py", line 379, in blob
    kms_key_name=kms_key_name,
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 164, in __init__
    name = _bytes_to_unicode(name)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\_helpers.py", line 389, in _bytes_to_unicode
    raise ValueError("%r could not be converted to unicode" % (value,))
ValueError: WindowsPath('sample.flac') could not be converted to unicode

--更新-- 尝试了snakecharmerb的方法,没有成功。这就是我得到的

Traceback (most recent call last):
  File "F:\cloud-speech-to-text\test.py", line 53, in <module>
    upload_blob('speech-demo-2',filename,remote_filename)
  File "F:\cloud-speech-to-text\test.py", line 23, in upload_blob
    blob.upload_from_filename(source_file_name)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 1127, in upload_from_filename
    content_type = self._get_content_type(content_type, filename=filename)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\google\cloud\storage\blob.py", line 647, in _get_content_type
    content_type, _ = mimetypes.guess_type(filename)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\mimetypes.py", line 291, in guess_type
    return _db.guess_type(url, strict)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\mimetypes.py", line 116, in guess_type
    scheme, url = urllib.parse.splittype(url)
  File "C:\Users\GIANNIS\AppData\Local\Programs\Python\Python37-32\lib\urllib\parse.py", line 956, in splittype
    match = _typeprog.match(url)
TypeError: expected string or bytes-like object

最佳答案

bucket.blob 需要一个字符串作为 blob_name 参数,但您要向它传递一个 Path 实例 - 具体来说,是一个 WindowsPath.

您可以通过调用 str 将路径转换为字符串。

>>> p = pathlib.Path('sample')
>>> fn = p.with_suffix('.flac')
>>> fn
WindowsPath('sample.flac') 
>>> str(fn)
'sample.flac'

所以这应该有效:

blob = bucket.blob(str(destination_blob_name))

关于python - WindowsPath 无法转换为 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54711053/

相关文章:

python - 函数中的参数 (`_` ) 是什么?

python - 如何在 pyparsing : match a set of words but not containing a given pattern 中为此编写语法

google-cloud-storage - 在GCS中编码或不编码路径部分?

python - 无法在 ML Engine 中导入 Google Cloud Storage 库

Node.js POST 文件到服务器

python - 将一个系列分配给 Pandas DataFrame 的多行

python - 循环中的python类

python - 如何将包含相同参数子集且值相同的 2 个函数合并为 1 个函数?

google-app-engine - GAE : What's faster loading an include config file from GCS or from cloud SQL

javascript - 当尝试在 firebase 函数中获取图像链接时, promise 返回未定义