1. 快速理解指定目录下的所有文件(目录)

  1. ```
  2. [[email protected]]# python3
  3. >>> import os
  4. >>> for file in os.listdir('.'):
  5. ... print(file)
  6. ```
  7. - [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory)

2. dict_items object has no attribute ‘sort’

  1. Python3 中,dict `dict.keys()`, `dict.items()` 以及 `dict.values()` 都返回的是“views 了,而不是返回 lists 了,所以 python2 中的代码:
  2. ```
  3. k = d.keys();
  4. k.sort()
  5. ```
  6. 应该被改成:
  7. ```
  8. k = sorted(d)
  9. ```
  10. - [dict_items object has no attribute 'sort'](https://stackoverflow.com/questions/28039155/dict-items-object-has-no-attribute-sort)

3. 解决 Python 中文编码错误的问题

  1. 在代码的 main 最开始加上这一段就好了:
  2. ```
  3. import sys
  4. if sys.version_info < (3, 0):
  5. # 2.x
  6. reload(sys)
  7. sys.setdefaultencoding('utf-8')
  8. elif sys.version_info <= (3, 3):
  9. import imp
  10. imp.reload(sys)
  11. else:
  12. import importlib
  13. importlib.reload(sys)
  14. ```

4. int 和 二进制 string 互转

  1. ```
  2. [[email protected]]# python
  3. >>> "{0:b}".format(255)
  4. '11111111'
  5. >>> int('11111111', 2)
  6. 255
  7. ```
  8. - [Python int to binary string?](https://stackoverflow.com/questions/699866/python-int-to-binary-string)
  9. - [Convert base-2 binary number string to int](https://stackoverflow.com/questions/8928240/convert-base-2-binary-number-string-to-int)