『Python标准库』学习笔记
来源:互联网 发布:csgofps优化cfg 编辑:程序博客网 时间:2024/06/13 17:44
1、关于apply的用法, 如果要使用字典参数, 而又元组为空的话, 元组也不能省略
python apply()函数
2、使用 __import__ 函数获得特定函数实现延迟( = = 虽然觉得并没有什么用, 大概好处是不用专门导入一个库?
def getfunctionbyname(module_name, function_name): module = __import__(module_name) return getattr(module, function_name)aa = getfunctionbyname('dumbdbm', 'open')#此时的aa已经是个函数了>>> type(aa)<type 'function'>据这篇博文: Python 三种导入模块的方法和区别 中提到的:
“两者的区别是,import 后面跟的必须是一个类型(type),而__import__() 的参数是一个字符串,这个字符串可能来自配置文件,也可能是某个表达式计算结果”然而还是并不觉得有什么用 = =
据这篇博文: python动态导入模块并reload类及函数 中提到:
“比如你希望加载某个文件夹下的所用模块,但是其下的模块名称又会经常变化时,就可以使用这个函数动态加载所有模块了,最常见的场景就是插件功能的支持。”
感觉这个作用还行 ╭(′▽`)╯
3、使用 __import__ 函数实现延迟导入
# 使用 __import__ 函数实现延迟导入class LazyImport: def __init__(self, module_name): self.module_name = module_name self.module = None def __getattr__(self, name): if self.module is None: self.module = __import__(self.module_name) return getattr(self.module, name)string = LazyImport('string')print string.lowercase</span>
# 使用 reload 函数import helloreload(hello)reload(hello)</span>注意, 当你重加载模块时, 它会被重新编译, 新的模块会代替模块字典里的老模
块. 但是, 已经用原模块里的类建立的实例仍然使用的是老模块(不会被更新).
同样地, 使用 from-import 直接创建的到模块内容的引用也是不会被更新的.
5、dir 返回由给定模块、类、实例, 或其他类型的所有成员组成的列表。
>>> aa = []>>> dir(aa)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__',</span>
6、vars 函数与此相似, 它返回的是包含每个成员当前值的字典. 如果你使用不带
参数的 vars , 它将返回当前局部名称空间的可见元素(同 locals() 函数 ).
>>> book = "library2"pages = 250scripts = 350print "the %(book)s book contains more than %(scripts)s scripts" % vars()the library2 book contains more than 350 scripts注: 这种做法的好处就是不用把需要输出的变量都放到一个额外的临时的字典里
7、isinstance 函数可以接受任何对象作为参数, 而issubclass函数在接受非类对象参数时会引发 TypeError 异常
8、eval 函数将一个字符串作为 Python 表达式求值
# os库
9、读取某文件夹的所有文件名
import osfor file in os.listdir("D:\\test"): print file
10、
import os# 获取当前目录cwd = os.getcwd()print "cwd:", cwd# 改变当前目录到指定目录os.chdir("D:\\test")print "cwd2:", os.getcwd()# 获取当前目录的父目录字符串名os.chdir(os.pardir)print "cwd3:", os.getcwd()
11、删除文件夹之前要先清空该文件夹下的文件
import os# 把旧的同名文件删除try: os.remove("D:\\test\\test1\\file") os.removedirs("D:\\test\\test1")except os.error: pass# 创建文件夹os.makedirs("D:\\test\\test1")fp = open("D:\\test\\test1\\file", "w")fp.write("This is a test")fp.close()
12、 os.removedirs :若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
13、os.makedirs: 可以创建多个目录级的文件
import osos.makedirs("D:\\test\\test1\\test2\\test3")
而 os.mkdir 则只可以创建单级目录, 如以下的例子则必须在上面的例子后才能成功创建, 否则会报错
os.mkdir("D:\\test\\test1\\test2\\test3\\test4")
14、 os.removedirs : 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
15、 os.rmdir : 删除单级空目录,若目录不为空则无法删除,报错
16、如果需要删除非空目录, 可以使用 shutil 模块中的 rmtree 函数
17、stat 函数可以用来获取一个存在文件的信息
它返回一个类元组对象(stat_result 对象, 包含 10 个元素), 依次是 st_mode (权限模式), st_ino (inode number), st_dev (device), st_nlink (number of hardlinks), st_uid (所有者用户 ID), st_gid (所有者所在组 ID ), st_size (文件大小, 字节), st_atime (最近一次访问时间), st_mtime (最近修改时间),st_ctime (平台相关; Unix 下的最近一次元数据/metadata 修改时间, 或者Windows 下的创建时间) - 以上项目也可作为属性访问
18、使用os模块调用其它程序(windows)
import osimport stringdef run(program, *args): #find executable for path in string.split(os.environ["PATH"], os.pathsep): file = os.path.join(path, program) + ".exe" print file try: return os.spawnv(os.P_WAIT, file, (file,) + args) except os.error: pass raise os.error, "cannot find executable"run("python", "hello.py")print "goodbye"
19、os.name : 判断现在正在实用的平台,Windows 返回 ‘nt'; Linux 返回’posix'
20、使用 os.path 模块处理文件名
import osfilename = "my/little/pony"print "using", os.name, "..."print "split", "=>", os.path.split(filename)print "splitext", "=>", os.path.splitext(filename)print "dirname", "=>", os.path.dirname(filename)print "basename", "=>", os.path.basename(filename)print "join", "=>", os.path.join(os.path.dirname(filename), os.path.basename(filename))
21、使用 os.listdir 搜索文件系统
# 使用 os.listdir 搜索文件系统import osdef index(directory): # like os.listdir, but traverses directory trees stack = [directory] files = [] while stack: directory = stack.pop() for file in os.listdir(directory): fullname = os.path.join(directory, file) files.append(fullname) if os.path.isdir(fullname) and not os.path.islink(fullname): # 判断是否是目录和不是链接 stack.append(fullname) return filesfor file in index("."): print file
22、os负责程序与操作系统的交互,sys负责程序与python解释器的交互
- 『Python标准库』学习笔记
- 《python标准库》学习笔记2
- Python标准库学习笔记-文本
- [代码笔记] python 常用标准库学习
- Python学习笔记--标准库篇
- python学习笔记4-python模块和标准库
- Python标准库笔记
- python学习标准库
- python学习标准库
- python标准库学习
- Python标准库学习
- 《python标准库》学习笔记1(__builtin__模块)
- Python学习笔记8:标准库之正则表达式
- python学习笔记(十)标准库pprint
- python学习笔记(十一)标准库sys
- python学习笔记(十二)标准库os
- python学习笔记(十三)标准库heapq
- python学习笔记(十四)标准库urllib
- 二维码问题
- unity5.x从入门到精通-7章
- dubbo简单入门例子
- 【算法】最近公共祖先(hihoCoder #1062)
- iOS判断耳机是否插入
- 『Python标准库』学习笔记
- iOS 8 之后的动态沙盒路径
- UITextField 限制值输入数字和英文
- explode_implode.php
- 设计模式之命令模式
- Saltstack 安装及认证
- saltstack grains
- 天气插件
- MyBatis传入多个参数的问题