pythonは2つの絶対パスをつなぎ合わせます
1002 ワード
os.path.join
はパッチ#!/usr/bin/env python3
import os
def two_abs_join(abs1, abs2):
"""
,
(windows C, D,E ... Linux /root / )
:param abs1:
:param abs2:
:return:
"""
# 1. ( \\ \)
abs2 = os.fspath(abs2)
# 2.
abs2 = os.path.splitdrive(abs2)[1]
# 3.
abs2 = abs2.strip('\\/') or abs2
return os.path.abspath(os.path.join(abs1, abs2))
user_home = r'E:\ftp\home\alex'
get_file_path = r"C:/a/alex.py"
t = two_abs_join(user_home, get_file_path)
print(t)
user_home = r'/user/path/alex/'
get_file_path = r"/root/alex.py"
t1 = two_abs_join(user_home, get_file_path)
print(t1)
E:\ftp\home\alex\a/alex.py
/user/path/alex/root/alex.py