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使用psutil获取进程信息的例子

psutil是什么 psutil是一个能够获取系统信息(包括进程、CPU、内存、磁盘、网络等)的Python模块。主要用来做系统监控,性能分析,进程管理,像glances也是基于psut...

使用Python代码实现Linux中的ls遍历目录命令的实例代码

使用Python代码实现Linux中的ls遍历目录命令的实例代码

一、写在前面   前几天在微信上看到这样一篇文章,链接为:https://www.jb51.net/it/692145.html,在这篇文章中,有这样一段话,吸引了我的注意:      ...

Python中偏函数用法示例

本文实例讲述了Python中偏函数用法。分享给大家供大家参考,具体如下: python中偏函数 当一个函数有很多参数时,调用者就需要提供多个参数。如果减少参数个数,就可以简化调用者的负...

Python 深入理解yield

只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: ˂!-- Code highlighting produced by Actipro CodeHighligh...

python实现淘宝购物系统

本文实例为大家分享了python淘宝购物系统的具体代码,供大家参考,具体内容如下 代码如下: #刚创建账户所拥有的钱 money = 0 #定义商品列表 goods_list =...