Python 加入目录路径
Python join directory paths
我对 os.path.join() 有疑问,因为它从不加入完整路径。
代码是:
get_base_dir = (os.getenv('BUILD_DIRECTORY'))
base_dir_path = pathlib.Path(get_base_dir)
print (base_dir_path ) # output is: F:\file\temp\ - which is correct
s_dir = '/sw/folder1/folder2/'
s_dir_path = pathlib.Path(s_dir)
print (s_dir_path) # output is: \sw\folder1\folder2\
full_path = os.path.join(base_dir_path, s_dir_path)
print (full_path) # output is: F:\sw\folder1'\folder2 instead of F:\file\temp\sw\folder1'\folder2
有人知道哪里出了问题吗?
此行为符合os.path.join
docs所述
Join one or more path components intelligently. The return value is
the concatenation of path and any members of *paths with exactly one
directory separator following each non-empty part except the last,
meaning that the result will only end in a separator if the last part
is empty. If a component is an absolute path, all previous components
are thrown away and joining continues from the absolute path
component.
(我加了重点)
我对 os.path.join() 有疑问,因为它从不加入完整路径。 代码是:
get_base_dir = (os.getenv('BUILD_DIRECTORY'))
base_dir_path = pathlib.Path(get_base_dir)
print (base_dir_path ) # output is: F:\file\temp\ - which is correct
s_dir = '/sw/folder1/folder2/'
s_dir_path = pathlib.Path(s_dir)
print (s_dir_path) # output is: \sw\folder1\folder2\
full_path = os.path.join(base_dir_path, s_dir_path)
print (full_path) # output is: F:\sw\folder1'\folder2 instead of F:\file\temp\sw\folder1'\folder2
有人知道哪里出了问题吗?
此行为符合os.path.join
docs所述
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
(我加了重点)