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

yipeiwu_com6年前Python基础

最近学习下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

相关文章

Python整型运算之布尔型、标准整型、长整型操作示例

Python整型运算之布尔型、标准整型、长整型操作示例

本文实例讲述了Python整型运算之布尔型、标准整型、长整型操作。分享给大家供大家参考,具体如下: #coding=utf8 def integerType(): '''''...

python 经典数字滤波实例

python 经典数字滤波实例

数字滤波分为 IIR 滤波,和FIR 滤波。 FIR 滤波: import scipy.signal as signal import numpy as np import pyla...

matplotlib简介,安装和简单实例代码

matplotlib简介,安装和简单实例代码

官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures i...

用python写的一个wordpress的采集程序

用python写的一个wordpress的采集程序

在学习python的过程中,经过不断的尝试及努力,终于完成了第一个像样的python程序,虽然还有很多需要优化的地方,但是目前基本上实现了我所要求的功能,先贴一下程序代码: 具体代码如...

windows下Virtualenvwrapper安装教程

windows下Virtualenvwrapper安装教程

windows下安装Virtualenvwrapper 我们可以使用Virtualenvwrapper来方便地管理python虚拟环境,但是在windows上安装的时候.....直接 i...