一、函数作用:
join() :将序列、字符串 、元组等中的元素以指定的字符连接生成一个新的字符串。
os.path.join() : 将多个路径组合后返回
二、join()方法说明:
join()方法
语法:
str.join(sequence)
参数说明:
str:指定的字符,即分隔符
sequence:需要连接的元素
实例:
#字符串序列
seq = ("apple", "banana", "pear")
str = ""
print(str.join(seq))
applebananapear
str = " "
print(str.join(seq))
apple banana pear
str = "-"
print(str.join(seq))
apple-banana-pear
output:
applebananapear
三、os.path.join() 函数说明
os.path.join() 函数
语法:
os.path.join(path1[,path2[,……]])
实例:
import os
path_root = 'F:\\data\\input'
dirs = os.listdir(path_root)
# 输出所有文件和文件夹
for file in dirs:
path = os.path.join(path_root,file)
path_test = os.path.join(path,'test')
#print(path)
print(path_test)
output:
F:\data\input\hamlet.txt
F:\data\input\hamlet2.txt
F:\data\input\input.txt
F:\data\input\test
F:\data\input\userurl_20150911.tdl