我得到一个 QString,它代表来自 QLineEdit 的目录。现在我想检查这个目录中是否存在某个文件。但是,如果我尝试使用 os.path.exists 和 os.path.join 并在目录路径中出现德语变音符号时遇到麻烦:
#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))
当您可能遇到德语变音符号时,您将如何检查文件是否存在?