用python可以很容易的完成,日常生活中的事情。在使用的过程中更多的是关注实际问题,而不需要过多的关注数据结构和编码,python已经集成很多有用的模块,还有一般常用到的数据结构。下面的是简单统计文件信息的代码。
from os import chdir, remove #
def scan(arg, dirname, names): #walk调用的函数三个默认参数
for file in names: #判断文件扩展名并写入到一个列表中
if file[-4:]==”.txt”: #
txtfile.append(join(dirname,file)) #
elif file[-4:]==”.exe”: #
exefile.append(join(dirname,file)) #
else: #
otherfile.append(join(dirname,file)) #
if __name__== “__main__”: #
otherfile=[] #
txtfile=[] #
exefile=[] #
path=”d:\” #
walk(path, scan, 0) #
print “txt file “,txtfile #打印文本文件名
print “exe file “,exefile #打印可执行文件名
print “other file”,otherfile #打印其余文件名
print len(txtfile)+len(exefile)+len(otherfile) #统计文件数
这个是参考网页
应用 Python 解决一些实际问题