Giter Club home page Giter Club logo

Comments (2)

benhoyt avatar benhoyt commented on May 20, 2024

This is (annoying) but expected behaviour, due to the way byte and unicode filenames are handled in Python 2.x. To get around it, just pass a unicode string instead of a byte string to scandir, like you're doing with open(), for example:

scandir.scandir(u"C:\Temp\xx鳭僣yy.txt")

Let me know if this works. Note that this is (or it should be!) the same behaviour as os.listdir() on Python 2.x.

from scandir.

benhoyt avatar benhoyt commented on May 20, 2024

Sorry, I said "filename" and copied the unicode filename rather than the unicode directory.

But no, my module isn't "broken". :-) It's operating by design, as per os.listdir(). The behaviour of bytes paths on Windows is kinda weird -- if you pass in a byte string, you get out byte strings with non-ASCII chars replaced by ? characters on Windows. This is different from on Linux, where you get UTF-8. So bytes paths are kind of half broken on Windows Python.

What you need to do is simply path a unicode path to scandir. Like so:

>>> os.mkdir('temp')
>>> f = open(u'temp\\xx\u9ced\u50e3yy.txt', 'w')  # create a unicode filename
>>> f.close()
>>> [e.name for e in scandir.scandir('temp')]  # this is what you are doing
['xx??yy.txt']
>>> [e.name for e in scandir.scandir(u'temp')]  # this is what you need to do
[u'xx\u9ced\u50e3yy.txt']
>>> [e.name.encode('utf-8') for e in scandir.scandir(u'temp')]  # or as UTF-8
['xx\xe9\xb3\xad\xe5\x83\xa3yy.txt']

Note that, by design, this exactly matches the behaviour of os.listdir() on Python 2.x:

>>> os.listdir('temp')
['xx??yy.txt']
>>> os.listdir(u'temp')
[u'xx\u9ced\u50e3yy.txt']

from scandir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.