Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:05:49+00:00 2026-06-04T06:05:49+00:00

I have a file with a unicode name(eg chinese characters). I get a UnicodeEncodeError.

  • 0

I have a file with a unicode name(eg chinese characters). I get a UnicodeEncodeError. I’m using postgres database with utf8 and the django development server on ubuntu lucid 64. What am I missing? I do the following where filename is the unicode name of the file in models.py:

def get_upload_path(instance,filename):
    return filename # Unicode error if filename has non latin 1 characters

class Kind (models.Model):
    style = models.ForeignKey(Style)
    kind_file = models.FileField(upload_to=get_upload_path)

from shell:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:05:50+00:00Added an answer on June 4, 2026 at 6:05 am

    I believe the problem is with your string formatting. In python2, it automatically converts between str type (which is a series of bytes) and unicode type, which represents the abstract series of unicode codepoints.

    I’m assuming that your filename is of type unicode.

    "tmp/%s/%s" is a byte string, so python will try to automatically encode your unicode into str to match. The problem is that it uses the ascii encoding to do so, which can’t hold your data.

    Changing your return statement to use temp2 instead of filename should work, since now you’re using the right types together.


    For the future, I’d also recommend watching the presentation I linked to in the comments, as it provides several strategies for avoiding this class of problem. The main one is that you should only use bytes when sending data outside your program. As soon as you receive bytes from the outside world, decode them to unicode, and only encode when you’re sending the data out of your program. You should also use unicode string literals internally (u"" instead of "").

    I’d also recommend more meaningful variable names than tempN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Unicode file (UTF-16 FFFE little-endian BOM) which contains rows of tab-separated
I have the following code reading a file containing unicode text (Japanese). File f
On OS-X (PHP5.2.11) I have a file: siësta.doc (and thousand other with Unicode filenames)
I have file locale/es/LC_MESSAGES/django.mo (and .po), ran makemessages and compilemessages. Definitely all messages are
I have a file foo.bar.1 on my server and when I try to access
Hi i am using django 1.2 .I my case i have few models which
I am designing a quiz engine by using django. In the models.py, I have
I would like to access a PHP file whose name has UTF-8 characters in
I have django installed on my server with pymongo and mongoengine.My django project settings.py
I'm working on Windows with Python 2.6.1. I have a Unicode UTF-16 text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.