簡體   English   中英

Windows下Python中的文件名格式

[英]Filename formatting in Python under Windows

我有兩個不同的文件,稱為:

'╠.txt'和'¦.txt'

這樣簡單的代碼:

files = os.listdir('E:\pub\private\desktop\')
for f in files:
    print f, repr(f), type (f)

會回來的

¦.txt '\xa6.txt' <type 'str'>
¦.txt '\xa6.txt' <type 'str'>

我不明白為什么我要為the字符而不是OxCC獲得代碼0xA6。 我一直在嘗試使用encode-decode方法,但是沒有成功。 我注意到sys.getfilesystemencoding()設置為mbcs-但我無法更改它,例如cp437。

很感謝任何形式的幫助。 謝謝!

您必須將unicode字符串傳遞給os.listdir ,Python將返回unicode文件名:

# a string that is unicode+raw (escapes \)
path = ur"E:\pub\private\desktop"
print os.listdir(path)
# [u'\xa6.txt', u'\u2560.txt']

Windows NT實際上使用unicode作為文件名,但是我猜Python在傳遞編碼的路徑名時會嘗試對它們進行編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM