遍历本地文件系统 (sys, os, path),例如写一个程序统计一个目录下所有文件大小并按各种条件排序并保存结果,代码如下:

#coding:GBK
import os; def SortList(item):
return item[1]; def ReadSize(fileName):
return float(os.path.getsize(fileName)); def WriteAll(path):
l = []
loger = open("test.log","w");
writer = open("path.txt","w");
reader = open("path.txt","r");
size = 0;
for root,dirs,files in os.walk(path):
for filesPath in files:
try:
fllePath = os.path.join(root,filesPath);
fileSize = float(ReadSize(fllePath)/1024);
size += fileSize;
x = (fllePath,int(fileSize));
l.append(x);
except:
loger.write("读取:"+os.path.join(root,filesPath)+"文件大小失败!\n");
continue;
l = sorted(l,key=SortList,reverse=True);
for item in l:
strTmp = "";
if float(item[1]/1024) > 1024:
strTmp = item[0]+" "+str(int(float(item[1]/1024/1024)))+"GB\n";
elif item[1] > 1024:
strTmp = item[0]+" "+str(int(float(item[1]/1024)))+"MB\n";
else:
strTmp = item[0]+" "+str(item[1])+"KB\n"; writer.write(strTmp);
writer.write("共使用磁盘空间:"+str(float(size/1024))+"MB");
loger.close();
writer.close();
print(reader.read());
reader.close(); fileName = os.getcwd();
WriteAll(fileName);
raw_input("END...");

Python之练习Demo的更多相关文章

  1. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  2. pyhanlp python 脚本的demo补充

    java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...

  3. Python HTML Resolution Demo - SGMLParser & PyQuery

    1. SGMLParser: 这里定义了一个Parse类,继承SGMLParser里面的方法.使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Pars ...

  4. Python简单多进程demo

    ''' 多线程使用场景: 怎样用Python的多线程提高效率? io操作不占用CPU 计算操作占用CPU Python多线程不适合CPU操作密集型的任务,适合io操作密集型的任务 如果有CPU操作密集 ...

  5. python - hadoop,mapreduce demo

    Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...

  6. python ros topic demo

    发布者: #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker():   ...

  7. 转 python trace walk DEMO

    https://blog.csdn.net/steadfast123/article/details/46965125 #quote from 'introduction to computation ...

  8. python UDP CS demo

    UDP Communication Contents UDP Communication Sending Receiving Using UDP for e.g. File Transfers Mul ...

  9. python spark kmeans demo

    官方的demo from numpy import array from math import sqrt from pyspark import SparkContext from pyspark. ...

  10. python选课系统demo的小练习

    #简化选课系统代码:先登陆,然后判断身份并实例化,根据身份对应的类,让用户选择 class Manager: operate_dict=[ ('创造学生账号',"creat_student& ...

随机推荐

  1. JavaScript高级程序设计49.pdf

    HTML5事件 contextmenu事件 contextmenu事件是冒泡的,可以将事件处理程序指定到document,这个事件的目标是用户操作的元素,在兼容DOM的浏览器中使用event.prev ...

  2. hdoj 1541 Stars【线段树单点更新+最大值维护】

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. 基于zabbix 内置key的应用

    一.内置key说明: Zabbix 内置了很多丰富的key,使得咱们再添加linux os模板的时候,已经帮我们把key给定义好,这样我们就能够直接链接模板就可以使用了. 我们这边的话列举一些内置ke ...

  4. nginx日志格式含义

  5. phpstorm 解决svn 无法提交的问题

    phpstorm 无法用svn 提交 提示如下错误: 网上找的解决办法 : 由于安装的TortoiseSVN工具,本身是带有command-line功能的(没有安装)如图: 使用Intellij ID ...

  6. NPOI组件集锦

    关于NPOI不错的介绍:http://www.cnblogs.com/restran/p/3889479.html http://www.cnblogs.com/Irving/archive/2012 ...

  7. 系统的启动模式(启动级别)的改动---使用upstart启动机制的

    /*********************************************************************  * Author  : Samson  * Date   ...

  8. JavaScript中七种函数调用方式及对应 this 的含义

    this 在 JavaScript 开发中占有相当重要的地位,不过很多人对this这个东西都感觉到琢磨不透.要真正理解JavaScript的函数机制,就非常有必要搞清楚this到底是怎么回事. 函数调 ...

  9. JavaRTS-DTraceProvider

    https://docs.oracle.com/javase/realtime/doc_2.0_u1/release/JavaRTSDTraceProvider.html

  10. 3 Ways of JDK Source Code Attachment in Eclipse---reference

    You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...