看板 Python 關於我們 聯絡資訊
各位先進好:請教有關絕對路徑的問題,程式碼如下。 import os script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in rel_path = "\html\9498886.html" abs_file_path = script_dir+rel_path abs_file_path2 = os.path.join(script_dir, rel_path) 檔案放在以下目錄。 D:\ides13\OneDrive\ipycode\claimsim\vx15 我使用「abs_file_path2 = os.path.join(script_dir, rel_path)」時, abs_file_path2之變數的值是:「D:\html\9498886.html」 但是「abs_file_path = script_dir+rel_path」的值,卻是正確的。 請問我什麼地方做錯了?是什麼原因造成這兩種差異?謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.133.32.131 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1595392225.A.839.html
TitanEric: 因為你rel path已經自己手加/了 path.join是要輸入一 07/22 12:48
TitanEric: 個資料夾 不是路徑 07/22 12:48
TitanEric: 也要看你script_dir位置是不是對的 07/22 12:49
ides13: 了解了,修正後已正確,謝謝。 07/22 13:08
TitanEric: 補充一下 join可以放路徑 但要放list然後加* 07/22 18:32