Python Snippet (一)

//注意Linux主机

//删除指定目录中文件名以 .tmp 为后缀的文件

 1 #!/usr/bin/python
 2 import os , glob
 3 dirname  =  ‘/tmp‘
 4 allpy  =  glob.glob(os.path.join(dirname,‘*‘))
 5 for filename in allpy:
 6     filesize  =  os.path.getsize(filename)
 7     if(filename.endswith(‘tmp‘)):
 8         try:
 9             os.remove(filename)
10         except:
11             continue

//输出指定文件夹(包括子文件夹)中最大的两个文件

 1 #!/usr/bin/python
 2 import os
 3 dirname = ‘/tmp‘
 4 allsize = []
 5 for (root,dirs,files) in os.walk(dirname):
 6     for filename in files:
 7         fullname  =  os.path.join(root,filename)
 8         filesize  =  os.path.getsize(fullname)
 9         allsize.append((filesize,fullname))
10 allsize.sort()
11 print allsize[-2:]

//删除指定文件(包括子文件夹)中的所有文件及文件夹

 1 #!/usr/bin/python
 2 import os
 3 dirname = ‘/tmp‘
 4 allsize = []
 5 for (root,dirs,files) in os.walk(dirname):
 6     for filename in files:
 7         fullname  =  os.path.join(root,filename)
 8         filesize  =  os.path.getsize(fullname)
 9         allsize.append((filesize,fullname))
10 allsize.sort()
11 print allsize[-2:]
时间: 2024-11-05 12:25:24

Python Snippet (一)的相关文章

Python snippet(代码片段)

一.字典访问 dict = {"age":18,"weight":65} print(dict.get("age"))print(dict.get("height")) # 由于字典dict中没有height这个键,返回的是默认值None. print(dict.get("height",0)) # 将默认值改为0,输出结果为0 和dict[key]的区别: dict.get(key)和dict[key]在

A look at WeChat security

原文地址:http://blog.emaze.net/2013/09/a-look-at-wechat-security.html TL;DR: Any (unprivileged) application installed on an Android phone can instruct WeChat to send an hash of your password to an external, attacker-controlled server. If you are a WeChat

vim-snippets Ultisnips的写法

vim的代码片段补全插件有名气的有vim-snipmate和Ultsnips,我的环境中snipmate因为冲突一直没法使用,所以使用的是Ultisnips.他们都是采用一样的vim-snippets做为配置文件,并且分别存放在对应名称的目录下.他们的格式基本上是相同的,但是也有少许差别,个人感觉ultisnips提供了更多的扩展. UltiSnipsEdit快速打开编辑配置文件,名字为<当前文件的扩展名>.snippets 基本格式: snippet <tigger> "

Snippet: Fetching results after calling stored procedures using MySQL Connector/Python

https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Python, you are calling a stored procedure which is also selecting data and you would like to fetch the rows of the result. Solution For this example we cr

python decorator 基础

转自:http://www.cnblogs.com/xybaby/p/6274187.html 一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类).首先来看一个简单的例子: # -*- coding: utf-8 -*- 2 def log_cost_time(func): 3 def wrapped(*args, **kwargs): 4 import time 5 begin = time.time() 6 try: 7 return func(*a

python decorator 进阶

转自:http://www.cnblogs.com/xybaby/p/6274283.html 上一篇文章开始的时候提到 "一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者参数)" 有一般情况,就有特殊情况.第一种特殊情况:装饰器可能也是一个类:第二种特殊情况:装饰器返回的对象的类型不一定等同于被装饰对象的类型. 对于第一种情况,我们知道对于任何callable的对象都可以进行调用(在对象名称后面使用小括号),callable对象的范围就比较广了

sublime text3 之snippet编写代码片段

sublime text 3 中有个强大的功能就是可以编写各种文件类型的snippet代码片段,可以节省大量的时间. 文件名为:jekyll-top.sublime-snippet(.sublime-snippet)后缀必须这样 <snippet> <content><![CDATA[/** * author:qinbb * title:智能推荐${1:标题} */ ${2}]]></content> <!-- Optional: Set a tabT

Exploring Python Code Objects

Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Inspired by David Beazley's Keynote at PyCon, I've been digging around in code objects in Python lately. I don't have a particular axe to grind, nor some

翻译:打造基于Sublime Text 3的全能python开发环境

原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/ 原文标题:Setting Up Sublime Text 3 for Full Stack Python Development 翻译:打造基于sublime text 3的全能Python开发环境 Sublime Text 3 (ST3) is lightweight, cross-platfo