os.listdir(path)Return a list containing the names of the entries in the directory given bypath. The list is in arbitrary order. It does not include the specialentries "." and ".." even if they are present in thedirectory.Availability: Unix, Windows.Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will bea list of Unicode objects. Undecodable filenames will still be returned asstring objects.
我都沒用過listdit。
但是,去找了下其使用說明:
os.listdir(path)Return a list containing the names of the entries in the directory given bypath. The list is in arbitrary order. It does not include the specialentries "." and ".." even if they are present in thedirectory.Availability: Unix, Windows.Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will bea list of Unicode objects. Undecodable filenames will still be returned asstring objects.所以:
你可以試試,傳入路徑是unicode,比如:
foundDirList = os.listdir(u"在這裡輸入你的")
然後,輸出的list中的檔名列表,就都是unicode了,就可以正常顯示出你要的,包括特殊字元的檔名了。
然後你就可以正常的打開了。
當然,後續處理檔案時,如果是中文等非ASCII的話,也是要了解涉及到字元編碼的。這時候,最好用codecs模組。如何使用,參見:
【教程】用Python的codecs處理各種字元編碼的字串和檔案
這裡不能貼地址,google搜標題即可找到帖子。