Tell me, please, why in the code below, the results inside the cycle are returned in Cyrillic, and after the cycle already in a different encoding?

# -*- coding: utf-8 -*- import os path_f = [] for d, dirs, files in os.walk(u'd:\удалить'): for f in files: path = os.path.join(d,f) path_f.append(path) print(path) print(path_f)` 

result

 d:\удалить\реестр1.xlsx d:\удалить\реестр2.xlsx [u'd:\\\u0443\u0434\u0430\u043b\u0438\u0442\u044c\\\u0440\u0435\u0435\u0441\u0442\u04401.xlsx', u'd:\\\u0443\u0434\u0430\u043b\u0438\u0442\u044c\\\u0440\u0435\u0435\u0441\u0442\u04402.xlsx'] 

1 answer 1

Because path is a string and output as a string, and path_f is a list, when you try to print it, it is serialized and the output is “like a binary” form. If you want to display the entire list as a set of strings, you need to make it explicitly convert it to a string. For example, like this:

 print('[%s]' % ','.join(path_f)) 

In this case, the output will be the same as yours (like a list dump), but the lines will be readable.

  • And if I want to write the result to a file? why with str (('[% s]'% ','. join (path_f)))) I get 'ascii' codec can't read characters in position 4-8: ordinal not in range (128)? - Alexander Kudryavtsev
  • ““ As if a binary ”form” —for clarity, this is not a binary form — it is a textual representation of the list (as it could be given in the source code as a constant). - jfs
  • @Alexander Kudryavtsev, and here we stick at once in two problems. The problem is number one: apparently, you are using the old version of the interpreter, and all 2.x are old for quite some time. If you are working with a new project, use the third branch. The second is purely to support those already written and working. - Safir
  • str(u'ф') because str(u'ф') should not be called on any version of Python (either an error or not). If you do not know how to save unicode to a file, ask a separate question. See the correct print to the python 2 Russian character file . Read the answer - jfs