Home  >  Article  >  Backend Development  >  Python中实现从目录中过滤出指定文件类型的文件

Python中实现从目录中过滤出指定文件类型的文件

WBOY
WBOYOriginal
2016-06-10 15:18:051449browse

最近学习下python,将从指定目录中过滤出指定文件类型的文件输出的方法总结一下,供日后查阅

复制代码 代码如下:

#!/usr/bin/env python

import glob
import os
os.chdir(“./”)
for file in glob.glob(“*.py”):
print file

print “#######Another One##########”

for file in os.listdir(“./”):
if file.endswith(“.py”):
print file

print “#######Another Two##########”
for root, dirs, files in os.walk(“./”):
for file in files:
if file.endswith(“.py”):
print os.path.join(root, file)

print “#######Another Three##########”

os.chdir(“./”)
filename_arr={}
i=0
for files in glob.glob(“*.py”):
filename_arr[i] = files
i += 1

for key, value in filename_arr.items():
print key, value

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn