Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WindowsPath could not be converted to unicode

I have the following python code that is trying to upload a file to a google cloud storage bucket and when i run it i get the following error

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

This my code

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)

I can't figure out what is wrong with it. Also my python knowledge is rudimentary at best. I just started learning python a week ago.

EDIT: I am including the full traceback

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

--UPDATE-- tried snakecharmerb's way and it didn't worked. This is what i get

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
like image 594
Yiannis Avatar asked Feb 05 '26 08:02

Yiannis


1 Answers

bucket.blob expects a string as it's blob_name argument, but you are passing it a Path instance - specifically, a WindowsPath.

You can convert the path to string by calling str on it.

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

So this ought to work:

blob = bucket.blob(str(destination_blob_name))
like image 68
snakecharmerb Avatar answered Feb 06 '26 22:02

snakecharmerb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!