WindowsPath无法转换为unicode

时间:2019-02-15 14:07:48

标签: python google-cloud-storage

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

 "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

-更新- 尝试了蛇charmerb的方式,但没有成功。这就是我得到的

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

1 个答案:

答案 0 :(得分:1)

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