作者ides13 (juso)
看板Python
标题[问题] 关於绝对路径。
时间Wed Jul 22 12:30:21 2020
各位先进好:请教有关绝对路径的问题,程式码如下。
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://webptt.com/cn.aspx?n=bbs/Python/M.1595392225.A.839.html
1F:推 TitanEric: 因为你rel path已经自己手加/了 path.join是要输入一 07/22 12:48
2F:→ TitanEric: 个资料夹 不是路径 07/22 12:48
3F:→ TitanEric: 也要看你script_dir位置是不是对的 07/22 12:49
4F:→ ides13: 了解了,修正後已正确,谢谢。 07/22 13:08
5F:→ TitanEric: 补充一下 join可以放路径 但要放list然後加* 07/22 18:32