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 7819227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:51:17+00:00 2026-06-02T06:51:17+00:00

I don’t seem to be able to open a file which has a unicode

  • 0

I don’t seem to be able to open a file which has a unicode filename. Lets say I do:

for i in os.listdir():
    open(i, 'r')

When I try to search for some solution, I always get pages about how to read and write a unicode string to a file, not how to open a file with file() or open() which has a unicode name.

  • 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-02T06:51:19+00:00Added an answer on June 2, 2026 at 6:51 am

    Simply pass open() a unicode string for the file name:

    In Python 2.x:

    >>> open(u'someUnicodeFilenameλ')
    <open file u'someUnicodeFilename\u03bb', mode 'r' at 0x7f1b97e70780>
    

    In Python 3.x, all strings are Unicode, so there is literally nothing to it.

    As always, note that the best way to open a file is always using the with statement in conjunction with open().

    Edit: With regards to os.listdir() the advice again varies, under Python 2.x, you have to be careful:

    os.listdir(), which returns filenames, raises an issue: should it return the Unicode version of filenames, or should it return 8-bit strings containing the encoded versions? os.listdir() will do both, depending on whether you provided the directory path as an 8-bit string or a Unicode string. If you pass a Unicode string as the path, filenames will be decoded using the filesystem’s encoding and a list of Unicode strings will be returned, while passing an 8-bit path will return the 8-bit versions of the filenames.

    Source

    So in short, if you want Unicode out, put Unicode in:

    >>> os.listdir(".")
    ['someUnicodeFilename\xce\xbb', 'old', 'Dropbox', 'gdrb']
    >>> os.listdir(u".")
    [u'someUnicodeFilename\u03bb', u'old', u'Dropbox', u'gdrb']
    

    Note that the file will still open either way – it won’t be represented well within Python as it’ll be an 8-bit string, but it’ll still work.

    open('someUnicodeFilename\xce\xbb')
    <open file 'someUnicodeFilenameλ', mode 'r' at 0x7f1b97e70660>
    

    Under 3.x, as always, it’s always Unicode.

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

Sidebar

Related Questions

Don't know if this has been asked before, so point me to another question
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't be to hard on me, this is my first try with JSF. I
Don't know if this has been answered before. Have custom routes to users. If
Don't exactly know what to search for: If i edit text in vim it
I don't know if this has been asked before, but what i'd like to
Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't think there's much to say, here's my code for (int i = 0;
Don't think this is a repost, difficult to search for the word between because
Lenses don't seem to have any disadvantages while having significant advantages over standard Haskell:

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.