python遍历指定类型的文件
作者:互联网
1、遍历以特定字符结尾的文件
root@PC1:/home/test# ls ## 测试文件类型 out1.csv out1.ped out2.csv out2.ped out3.csv out3.ped test1.txt test2.txt test3.txt traverse_file.py root@PC1:/home/test# cat traverse_file.py import os for i in os.listdir(): ##此处的i是文件名形式的字符串,可以依据字符串的特征进行筛选 if i.endswith(".ped"): ## 遍历以.end结尾的文件 print(i) root@PC1:/home/test# python3 traverse_file.py out1.ped out2.ped out3.ped
2、遍历以特定字符开头的文件
root@PC1:/home/test# ls ## 测试文件 out1.csv out1.ped out2.csv out2.ped out3.csv out3.ped test1.txt test2.txt test3.txt traverse_file.py root@PC1:/home/test# cat traverse_file.py import os for i in os.listdir(): if i.startswith("out"): ## 遍历以out开头的文件 print(i) root@PC1:/home/test# python3 traverse_file.py out3.csv out1.ped out2.csv out1.csv out2.ped out3.ped
标签:out1,out2,遍历,traverse,python,指定,out3,ped,csv 来源: https://www.cnblogs.com/liujiaxin2018/p/15583759.html