str(file.key) = ‘1011/101011/file_name’
newFileName = str(file.key)
But, when I run the code i get:
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position
x-y: ordinal not in range(128)
I need to do some parsing on the file name and then download it from s3 server.
How do I get just ‘file_name’?
You’ve posted far to little context to give a decent answer, but I’ll try anyway.
The filename you are trying to create seems to contain non-ascii characters, which cannot be automatically be converted into a standard str in python 2.x.
If you replace
strwithunicodeyou can avoid the need for conversion alltogether. If some other part of your code requires you to use an str, you could try to encode it like this:newFileName = unicode(file.key).encode('ascii', 'ignore'). Note that unconvertable characters will be omitted in my example.