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使用点操作符访问字典(dict)数据的方法

本文实例讲述了python使用点操作符访问字典(dict)数据的方法。分享给大家供大家参考。具体分析如下: 平时访问字典使用类似于:dict['name']的方式,如果能通过dict.n...

Python实现查找匹配项作处理后再替换回去的方法

本文实例讲述了Python实现查找匹配项作处理后再替换回去的方法。分享给大家供大家参考,具体如下: 这里实现Python在对找到的匹配项进行适当处理后,再替换掉原来那个匹配的项。 #...

python去除字符串中的换行符

今天写这个,要用python去除字符串中的换行符并写入文件,网上查阅,就一句代码replace("\n",""),加上之后,搞了半天,还是不对。 以上是我今天遇到的问题,以下是解决方案。...

Python中py文件引用另一个py文件变量的方法

最近自己初学Python,在编程是遇到一个问题就是,怎样在一个py文件中使用另一个py文件中变量,问题如下: demo1代码 import requests r = requests...

python绘制直方图和密度图的实例

python绘制直方图和密度图的实例

对于pandas的dataframe,绘制直方图方法如下: //pdf是pandas的dataframe, delta_time是其中一列 //xlim是x轴的范围,bins是分桶个...