在Windows上使用Python 2.6我有一個函數需要接受路徑名作爲arg。當某些特定路徑通過時,我遇到了問題。消毒用戶輸入路徑名稱
C:\users\bob\something.png #this is handled no prob.
C:\users\bob\nothing.png #this generates a WindowsError
C:\users\bob\test.png #this also generates a WindowsError
什麼我收集的是,在「一無所有」路徑\n
被解釋爲一個新的生產線,並在「測試」路徑\t
被解釋爲一個標籤。
如果我打印出路徑名稱,那就是正在發生的情況。
print os.path.abspath("C:\users\bob\nothing.png")
C:\users\bob
othing.png
與「測試」路徑相同,除了選項卡而不是新行。
我到目前爲止唯一想做的是檢查\n
或\t
是否在路徑名中,然後相應地處理它,但必須有更好的方法。
if '\n' in path_name:
#escape slash, move on
更好的方法是什麼?
這與路徑無關;它似乎是關於Python源文件中的字符串引用。 – 2015-12-11 22:49:43