Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.walk() strips polish characters

Tags:

python

So what I'm trying to do is fix some id3tags of mp3 files. It all works, except for files with any kind of accent, because os.walk seems to strip them.

For example, I have the file 01.Co Słychać.mp3, which in this code:

for root, dirs, files in os.walk(folder):
    print files

Shows up as ['01.Co Slychac.mp3'], later resulting in a 'No such file or directory' error.

How can this be fixed?

like image 458
Robus Avatar asked Sep 25 '11 12:09

Robus


1 Answers

Did you define folder as a Unicode string? This has implications on how os.walk() matches its subdirectories, or better, the type of string that it returns.

>>> for a,b,c in os.walk("."):
...  print b
...  break
...
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'tcl', 'Tools']
>>> for a,b,c in os.walk(u"."):
...  print b
...  break
...
[u'DLLs', u'Doc', u'include', u'Lib', u'libs', u'tcl', u'Tools']
like image 163
Tim Pietzcker Avatar answered Sep 19 '22 12:09

Tim Pietzcker