os.listdir()得到的是个list,这个list中的元素(就是字符串)的编码有时是utf8,有时是gb2312,那么这个编码方式是根据什么确定的呢?如果我想让os.listdir()获取字符串的时候以utf8编码,应该怎么设置呢?
有时是utf8?你是怎么得到这个结论的?
参考python documentation的os部分:
是不是utf8是根据你给的path的类型来决定的,如果path是unicode,那返回的结果也都是unicode
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.