python中的部分函数
来源:互联网 发布:网络歌手田力子 编辑:程序博客网 时间:2024/06/05 21:01
在拼接路径的时候用的。举个例子,
os.path.join(“home”, "me", "mywork")
在Linux系统上会返回
“home/me/mywork"
在Windows系统上会返回
"home\me\mywork"
好处是可以根据系统自动选择正确的路径分隔符"/"或"\"
二、 range与xrange
xrange 用法与 range 完全相同,所不同的是xrange生成的不是一个list对象,而是一个生成器
>>> range(5) [0, 1, 2, 3, 4] >>> range(1,5) [1, 2, 3, 4] >>> range(0,6,2)[0, 2, 4]>>> xrange(5)xrange(5)>>> list(xrange(5))[0, 1, 2, 3, 4]>>> xrange(1,5)xrange(1, 5)>>> list(xrange(1,5))[1, 2, 3, 4]>>> xrange(0,6,2)xrange(0, 6, 2)>>> list(xrange(0,6,2))[0, 2, 4]由上面的示例可以知道:要生成很大的数字序列的时候,用xrange会比range性能优很多,因为不需要一上来就开辟一块很大的内存空间。
xrange 和 range 这两个基本上都是在循环的时候用。
三、tarfile模块
使用tarfile压缩
import tarfile #创建压缩包名tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")#创建压缩包for root,dir,files in os.walk("/tmp/tartest"): for file in files: fullpath = os.path.join(root,file) tar.add(fullpath)tar.close()
使用tarfile解压
def extract(tar_path, target_path): try: tar = tarfile.open(tar_path, "r:gz") file_names = tar.getnames() for file_name in file_names: tar.extract(file_name, target_path) tar.close() except Exception, e: raise Exception, e其中open的原型是:
tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)mode的值有:
'r' or 'r:*' Open for reading with transparent compression (recommended).'r:' Open for reading exclusively without compression.'r:gz' Open for reading with gzip compression.'r:bz2' Open for reading with bzip2 compression.'a' or 'a:' Open for appending with no compression. The file is created if it does not exist.'w' or 'w:' Open for uncompressed writing.'w:gz' Open for gzip compressed writing.'w:bz2' Open for bzip2 compressed writing.
未完,待整理。。
0 0
- python中的部分函数
- python中的部分函数
- python中的list函数(部分截取)
- 可爱的 Python : Python中的函数式编程,第二部分
- JQuery中的部分函数
- MFC中的部分函数
- python numpy的部分函数
- 俄罗斯方块中的部分函数及。。。。。。。。
- [Python]Python函数参数中的*,**
- Python:python中的随机数函数
- python中的exec 函数
- python中的函数对象
- python中的lambda函数
- Python中的一些函数
- python中的reduce函数
- Python函数参数中的*,**
- Python中的range()函数
- python中的专有函数
- Remove Duplicates from Sorted List I II
- windows下安装elasticsearch
- React Native 性能优化之可取消的异步操作
- 自己的脚本、自己的PS、自己的元素、自己的字体
- iOS 又遇到小坑
- python中的部分函数
- Android系统编程思想篇:单例模式
- .NET入行之工作前
- TCP协议
- 01.JS操作JSON总结
- hashCode与对象equals之间的使用
- set insert () 3
- Servlet 生命周期、工作原理
- js 验证 身份证号