02 2013 档案
摘要:变量作用域:LEGB L: local E: enclosing function locals G: global B: built-in 1。 内嵌的模块是全局作用域:顶层命名空间 2。 全局作用域的范围只限于单个文件 3。 每次对函数的调用都创建了一个新的本地作用域 4。 所有的变量名都可以归为 本地、全局、内置def func1(param=None): def func2(param2=param): if not param2: param2 = 'default' print param2 # Just...
阅读全文
摘要:test_module.py'''Created on Feb 28, 2013@author: agent'''import sysname = 42def func(path): print pathclass Klass: passprint 'done loading.'if __name__ == '__main__': passtest:import test_moduleprint test_module.__dict__.keys()print test_module.__file__print t
阅读全文
摘要:# 1. import modulemodule.method()del module # get rid of the module name(name space)reload(module)# 2. import run.py dynamicallyimport imp, osm = imp.load_source('m', os.path.join(_casedir, 'run.py')) # 3.from module import methodmethod()# 4. import ctypesobj = ctypes.CDLL('XXX.d
阅读全文
摘要:'''Created on Feb 7, 2013@author: changxue@summary: personal utils'''import os, time, shutil#current_time = time.strftime('%Y-%m-%d_%H%M', time.localtime(time.time()))current_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))logfile = os.path.join(&
阅读全文
摘要:'''Created on Feb 22, 2013@author: changxue@summary: aim: generate jar file path: 1. copy original jar to target dir 2. java t...
阅读全文
摘要:'''Created on Feb 22, 2013@author: changxue@summary: extract all archive files in src_dir into target_dir'''import os, shutil_list_postfix = ['.rar', '.zip', '.jar']tool_path = os.path.join(r'C:\Program Files\7-Zip')from common_util import _cre
阅读全文
摘要:python3: https://docs.python.org/3/library/csv.html
阅读全文
摘要:def test_variable_forin(): alist = ['a', 'bb', 'cc', 'dd'] count = 0 for i in alist: print i count += 1 if len(i) > 1: ...
阅读全文
摘要:临时文件definition: import tempfile tempfile_name = tempfile.mktemp() # 创建名称唯一的临时文件供使用 temp1 = tempfile.TemporaryFile() # 注意:用TemporaryFile()创建的文件没有文件名 temp11= tempfile.TemporaryFile(mode='w+t') temp2 = tempfile.NamedTemporaryFile() # 尽管文件带有名字,但它仍然会在close后自动删除 temp...
阅读全文
摘要:stdin: 0, stdout: 1, stderr: 2例2:(注cmd重定向进入后用exit返回原目录) cmd > file 把 stdout 重定向到 file 文件中 cmd >> file 把 stdout 重定向到 file 文件中(追加) cmd 1> file 把 stdout 重定向到 file 文件中 cmd > file 2>&1 把 stdout 和 stderr 一起重定向到 file 文件中 cmd 2> file 把 stderr 重定向到 file 文件中 cmd 2>> file 把 stder
阅读全文
摘要:summary:import timecur_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))一、小应用1.python获取当前时间time.time() 获取当前时间戳 1180759620.859time.localtime() 当前时间的struct_time形式 (2007, 6, 2, 12, 47, 7, 5, 153, 0)time.ctime() 当前时间的字符串形式 ’Sat Mar 28 22:24:24 2009′ISOTIMEFORMAT=’%Y-%m-%d %X
阅读全文
摘要:'''Created on Jan 28, 2013@author: changxue@summary: to look for a file by filename within specified directory, or look for some word in files'''import osimport sysdef search_file(key, scope): for parent, dirs, files in os.walk(scope): if key in files: print parent, ...
阅读全文
摘要:'''Created on Feb 6, 2013@author: changxue@summary: to delete all files and directories in specified directory. print the name of file or directory if it can't remove.'''import os, sysdef clean(path): # to deal with abnormal situation if not os.path.exists(path): raise Ex
阅读全文

浙公网安备 33010602011771号