Python os.path.splitext()的用法。
第一点 定义内容
#定义参数内容
... import os
path1='E:\test\6.txt'#文件路径
path2='E:\test'#目录
#用splitext()方法切割
... split_path1=os.path.splitext(path1)
split_path2=os.path.splitext(path2)
#打印结果
... print(split_path1)#正常切割
('E:\test\x06', '.txt')
print(split_path2)#目录切割后异常
('E:\test', '')
第二种 统计
file_number=0
for root,dirs,files in os.walk("e:\\"):
... for file in files:
... if os.path.splitext(file)[1]==".txt":#将对应的文件与文件名分割
... file_number+=1
... #print (file)
...
print (file_number)