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中获得当前目录和上级目录的实现方法

获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file__ 为当前文件...

windows下python虚拟环境virtualenv安装和使用详解

前面介绍了python在ubuntu16.04环境下,python的虚拟环境virtualenv的安装,下面介绍在windows环境下的安装和使用。 环境信息 操作系统:windo...

Python读写Excel文件的实例

Python读写Excel文件的实例

最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理。首先贴出网上找来的读写Excel的脚本。 1.读取Excel(需要...

python通过ffmgep从视频中抽帧的方法

如下所示: ffmpeg中文文档:http://linux.51yip.com/search/ffmpeg ffmpeg -i test_baofeng.wmv -y -f image2...

python 自动重连wifi windows的方法

如下所示: # coding=utf-8 import urllib2 import urllib from cookielib import CookieJar import os...