0

I need to get a list of files in a directory, where the filenames have to be unicode.

The following code gives me a list of files.

files = [ f for f in listdir(filedirectory) if isfile(join(filedirectory,f)) ]
for fi in files:
    print(fi)
    print(type(fi))

Result:

AT10_nachmittags.JPG
<type 'str'>
AT11_nachmittags.JPG
<type 'str'>
AT12_nachmittags.JPG
<type 'str'>
AT2_morgens2.JPG
<type 'str'>
AT3_morgens.JPG
<type 'str'>
AT_morgens.JPG
<type 'str'>
Bühne.JPG
<type 'str'>
Bühnenauftritt.JPG
<type 'str'>

The problem is that these are not unicode. I need them to be unicode so I can write them in a file because they include german "umlaute" unicode symbols.

I'm working on Mac OS X 10.10.4 with python 2.7.6

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Luke
  • 1,768
  • 2
  • 20
  • 30

1 Answers1

3

Try to execute listdir with filedirectory as unicode string argument. E.g. filedirectory = u'/some/dir'

From documentation:

os.listdir(path) Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.

Availability: Unix, Windows.

> Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects.

svfat
  • 3,273
  • 1
  • 15
  • 34
  • try to switch to Python 3 – svfat Aug 17 '15 at 08:33
  • @Luke I’m returning OS X with python 2.7.10 and it returns Unicode strings for me. – xrisk Aug 17 '15 at 08:36
  • @Luke: check again. Are you sure that `isinstance(filedirectory, unicode)` is true just before you pass `filedirectory` to `os.listdir()`? – jfs Aug 17 '15 at 09:37
  • 1
    @svfat: undecodable names are unlikely on OSX where utf-8 is enforced for filenames. And all names in the question are decodable anyway. – jfs Aug 17 '15 at 09:39