1
我得到一個QString代表QLineEdit中的一個目錄。現在我想檢查這個目錄中是否存在某個文件。但如果我嘗試這與os.path.exists和os.path.join和惹上麻煩時,德國的變音符號出現在目錄路徑:如何檢查使用德語變音符號的QString代表的路徑?
#the direcory coming from the user input in the QLineEdit
#i take this QString to the local 8-Bit encoding and then make
#a string from it
target_dir = str(lineEdit.text().toLocal8Bit())
#the file name that should be checked for
file_name = 'some-name.txt'
#this fails with a UnicodeDecodeError when a umlaut occurs in target_dir
os.path.exists(os.path.join(target_dir, file_name))
你將如何檢查文件是否存在,時可能會遇到德語變音器?
我現在還沒有在我的工作,嘗試的事情了,但我肯定會給你的方法一試。此外,我發現這一點,它提供完全讓PyQt檢查文件和處理變音符號。 http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qfileinfo.html#exists – mamachanko 2010-04-14 14:55:17