参考链接: os.path.join(path, *paths)
代码实验展示:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import os
>>> result = os.path.join(r'a')
>>> print(type(result),result,sep='\n')
<class 'str'>
a
>>> result = os.path.join(r'a',r'b')
>>> print(type(result),result,sep='\n')
<class 'str'>
a\b
>>> type(result),result
(<class 'str'>, 'a\\b')
>>> result = os.path.join(r'a',r'b','')
>>> type(result),result
(<class 'str'>, 'a\\b\\')
>>>
>>> print(type(result),result,sep='\n')
<class 'str'>
a\b\
>>>
>>> result = os.path.join(r'\a',r'b','')
>>> print(type(result),result,sep='\n')
<class 'str'>
\a\b\
>>> result = os.path.join(r'a',r'C:\Users\chenxuqi\Desktop\干扰',r'b')
>>> print(type(result),result,sep='\n')
<class 'str'>
C:\Users\chenxuqi\Desktop\干扰\b
>>>
>>> result = os.path.join(r'C:\Users\chenxuqi\Desktop\图片',r'C:\Users\chenxuqi\Desktop\干扰',r'b')
>>> print(type(result),result,sep='\n')
<class 'str'>
C:\Users\chenxuqi\Desktop\干扰\b
>>> result = os.path.join(r'C:\Users\chenxuqi\Desktop\图片',r'C:\Users\chenxuqi\Desktop\干扰',r'\b')
>>> print(type(result),result,sep='\n')
<class 'str'>
C:\b
>>> result = os.path.join(r'aaa',r'ffff',r'\b')
>>> print(type(result),result,sep='\n')
<class 'str'>
\b
>>> result = os.path.join(r'aaa',r'ffff',r'b')
>>> print(type(result),result,sep='\n')
<class 'str'>
aaa\ffff\b
>>> result = os.path.join(r'C:\Users\chenxuqi\Desktop\图片',r'\b',r'C:\Users\chenxuqi\Desktop\干扰')
>>> print(type(result),result,sep='\n')
<class 'str'>
C:\Users\chenxuqi\Desktop\干扰
>>>
>>>