把学到的都整理到zutil.py里吧
-
#!/bin/env python
-
#coding:utf8
-
# filename: zutil.py
-
# description: util for myself
-
# createtime: 2013/10/11
-
# last modify: 2013/10/11
-
-
# open file
-
import random
-
import sys
-
def readfile(filename):
-
f = open(filename)
-
# f = open(filename, 'a') #append
-
# f = open(filename, 'w') # write
-
f.write(str(random.randint(0,9)) + '\n')
-
f.flush()
-
f.close()
-
-
def redirectoutput():
-
sys.stdout = open("stdout.txt", 'w')
-
#!/bin/env python
-
#coding:utf8
-
# filename: zutil.py
-
# description: util for myself
-
# createtime: 2013/10/11
-
# last modify: 2013/10/11
-
-
# open file
-
import random
-
import sys
-
def readfile(filename):
-
f = open(filename)
-
# f = open(filename, 'a') #append
-
# f = open(filename, 'w') # write
-
f.write(str(random.randint(0,9)) + '\n')
-
f.flush()
-
f.close()
-
-
def redirectoutput():
-
sys.stdout = open("stdout.txt", 'w')
import hashlib
import os
# from a[.py] import something
def getFileMd5(filename):
myhash = hashlib.md5()
if not os.path.isfile(filename):
return
f = open(filename)
while True:
b = f.read(8096)
if not b:
break
myhash.update(b)
return myhash.hexdigest()
# return myhash.hexdigest().upper()
# file size
# os.path.getsize(filename)
经验:
1. 在python里,函数的默认值是在函数定义的时候实例化的,而不是在调用的时候
阅读(163) | 评论(0) | 转发(0) |