Python之练习Demo
遍历本地文件系统 (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的更多相关文章
- RPi 2B python opencv camera demo example
/************************************************************************************** * RPi 2B pyt ...
- pyhanlp python 脚本的demo补充
java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...
- Python HTML Resolution Demo - SGMLParser & PyQuery
1. SGMLParser: 这里定义了一个Parse类,继承SGMLParser里面的方法.使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Pars ...
- Python简单多进程demo
''' 多线程使用场景: 怎样用Python的多线程提高效率? io操作不占用CPU 计算操作占用CPU Python多线程不适合CPU操作密集型的任务,适合io操作密集型的任务 如果有CPU操作密集 ...
- python - hadoop,mapreduce demo
Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...
- python ros topic demo
发布者: #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker(): ...
- 转 python trace walk DEMO
https://blog.csdn.net/steadfast123/article/details/46965125 #quote from 'introduction to computation ...
- python UDP CS demo
UDP Communication Contents UDP Communication Sending Receiving Using UDP for e.g. File Transfers Mul ...
- python spark kmeans demo
官方的demo from numpy import array from math import sqrt from pyspark import SparkContext from pyspark. ...
- python选课系统demo的小练习
#简化选课系统代码:先登陆,然后判断身份并实例化,根据身份对应的类,让用户选择 class Manager: operate_dict=[ ('创造学生账号',"creat_student& ...
随机推荐
- Tornado自定义分布式session框架
一.session框架处理请求执行的流程: 1.服务器端生成随机的cookie字符串 2.浏览器发送请求,服务器将cookie返回给浏览器. 3.服务器在生成一个字典.字典的key为cookie,va ...
- 如何使用chown?
以test为例,目前test归root用户拥有,也归root组拥有 [root@localhost home]# ls -al total 36 drwxr-xr-x 6 root ro ...
- CentOS搭建GIT服务器【二】-HTTP源码访问及smart http协议
搭建完git之后,我们希望可以在线看见源码,以及使用http协议上传下载源码. 安装gitweb.httpd: yum install gitweb yum install httpd gitweb默 ...
- javascript函数定义表达式和函数声明的区别
在javascript中,函数有两种定义写法,函数定义表达式和函数声明,其例子分别如下所示: var test = function(x){ return x; } function test(x){ ...
- windows mobile 6.5 隐藏 左下角(左上角)的开始按钮 叉号关闭按钮
其实做起来很简单,但是国内的网站就是找不到. 1.开始按钮原来的界面是这样的: windows mobile 6.0界面: windows mobile 6.5.X界面: 修改一个windows mo ...
- S2SH+Hibernate search出现的问题
一 java.lang.NoSuchMethodError: org.hibernate.engine.transaction.spi.TransactionEnvironment.getJtaPl ...
- java_method_日期方法
package cn.com.qmhd.tools; import java.text.SimpleDateFormat; import java.util.Calendar; import java ...
- mysql调优 基础
MySQL调优可以从几个方面来做: 1. 架构层:做从库,实现读写分离: 2.系统层次:增加内存:给磁盘做raid0或者raid5以增加磁盘的读写速度:可以重新挂载磁盘,并加上noatime参数,这样 ...
- 在网上浏览.NET的所有代码,并且让你的Visual Studio的go to definition(F12)指向在线代码
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在网上浏览.NET的所有代码,并且让你的Visual Studio的go to definition(F ...
- c pointer and array
Pointer: A pointer is a variable that contains the address of a variable. if c is a char and p is a ...