遍历本地文件系统 (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. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf1

    canvas元素可以说是HTML5元素中功能最强大的一个,它真正的能力是通过Canvas的context对象(绘图上下文)表现出来的 fillText()方法使用fillStyle属性来填充文本中的字 ...

  2. std::move()和std::forward()

    std::move(t)负责将t的类型转换为右值引用,这种功能很有用,可以用在swap中,也可以用来解决完美转发. std::move()的源码如下 template<class _Ty> ...

  3. 我的第一个JApplet-绘制笑脸

    初学Java,有很多东西都不太理解,但是我想以前初学C语言的时候也是不太懂,先参考着书上的程序写,然后用多了就自然而然的懂了! 下面来简单的介绍一下我自学的第一个Java小应用程序-绘制笑脸,下面是源 ...

  4. usaco 猜数游戏

    Description 为了提高智商,锻炼思维能力,奶牛设计了一个猜数游戏.游戏开始前,贝西会在牛棚后面摆上N个数字.所有数字排成一条直线,按次序从1到N编号.每个数字在1到10^9之间,没有两个数字 ...

  5. Django中的ORM进阶操作

    Django中的ORM进阶操作 Django中是通过ORM来操作数据库的,通过ORM可以很easy的实现与数据库的交互.但是仍然有几种操作是非常绕也特别容易混淆的.于是,针对这一块,来一个分类总结吧. ...

  6. phpDesigner 7.2.5 注册码 更改 语法高亮 主题

    注册码: 用户名:www.xiazaiba.com 序列号:43AB0D432A29EE238CCE0F884D84D8A18498498E98298A98568AD05A0B40 验证码:7S2FF ...

  7. Using Java SecurityManager to grant/deny access to system functions

    In Java it is possible to restrict access to specific functions like reading/writing files and syste ...

  8. CodeIgniter开发实际案例-新闻网站【转】

    CodeIgniter开发实际案例-新闻网站 转:http://blog.csdn.net/ict2014/article/details/22104711?utm_source=tuicool&am ...

  9. js 设置cookie

    function GetCookieVal(offset) // 获得Cookie解码后的值 { var endstr = document.cookie.indexOf(";", ...

  10. 通过strace 监控 fdatasync

    通过strace 监控 Redis AOF文件的系统调用 Redis中主要的AOF设置 「appendonly yes」 开启每次更新操作后进行日志记录 「appendfilename appendo ...