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

相关文章

Django 接收Post请求数据,并保存到数据库的实现方法

要说基本操作,大家基本都会,但是有时候,有些操作使用小技巧会节省很多时间。 本篇描述的就是使用dict小技巧,保存到数据库,用来节省大家编码的工作量。 主要内容:通过for循环拿到pos...

python 处理dataframe中的时间字段方法

在机器学习过程中,通常会通过pandas读取csv文件,保持成dadaframe格式,然而有时候需要对dataframe中的时间字段进行数据建模,比如时间格式为datetime,那么像一...

Python全局变量操作详解

接触Python时间不长,对有些知识点,掌握的不是很扎实,我个人比较崇尚不管学习什么东西,首先一定回去把基础打的非常扎实了,再往高处走。今天遇到了Python中的全局变量的相关操作,遇到...

Python3实现获取图片文字里中文的方法分析

本文实例讲述了Python3实现获取图片文字里中文的方法。分享给大家供大家参考,具体如下: 一、运行环境 (1) win10 (2) pycharm (3) python 3.5 (4)...

python numpy 一维数组转变为多维数组的实例

python numpy 一维数组转变为多维数组的实例

如下所示: import numpy new_list = [i for i in range(9)] numpy.array(new_list).reshape(3,3) 借助n...