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& ...
随机推荐
- wxWidgets一个界面与数据分离的简单例子
/*************************************************************** * Name: MyApp.h * Purpose: Defines ...
- ZOJ Problem Set - 1025解题报告
ZOJ Problem Set - 1025 题目分类:基础题 原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=10 ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【转】shell 教程——04 什么时候使用Shell
因为Shell似乎是各UNIX系统之间通用的功能,并且经过了POSIX的标准化.因此,Shell脚本只要“用心写”一次,即可应用到很多系统上.因此,之所以要使用Shell脚本是基于: 简单性:Shel ...
- 图解三种APP的区别
- 闪回还原点(Flashback Restore Point)
Flashback Restore Point(闪回还原点) 闪回还原点分两种,一种是Normal Restore Points(正常还原点),另一种是Guaranteed Restore Point ...
- 01 if
if - else if语句是一种控制语句,执行一代码块,如果一个表达式计算为true if (expression) statement1 else statement2 如 ...
- [Redux] Using mapDispatchToProps() Shorthand Notation
We will learn how to avoid the boilerplate code in mapDispatchToProps() for the common case where ac ...
- PreferenceActivity的使用
PreferenceActivity是一个非常有用的基类,当我们开发Android项目时避免不了选项设置,这些设置习惯用Preference来保存.Android专门为这种Activity提供了便捷的 ...
- SQL Server存储内幕系列
http://blog.itpub.net/355374/list/1/?cid=75087