day6 SYS模块
SYS模块
用于提供对Python解释器相关的操作:
(1)sys.argv 命令行参数List,第一个元素是程序本身路径
>>> sys.argv
['']
(2)sys.exit(n) 退出程序,正常退出时exit(0)
(3)sys.version 获取Python解释程序的版本信息
(4)sys.maxint 最大的Int值
(5)sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
>>> sys.path
['', '/usr/local/lib/python3.5/dist-packages/pygame-1.9.4.dev0-py3.5-linux-x86_64.egg', '/usr/lib/python35.zip', '/usr/lib /python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/zhuzhu/.local/lib/python3.5 /site-packages', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
(6)sys.platform 返回操作系统平台名称
>>> sys.platform
'linux'
(7)sys.stdin 输入相关
(8)sys.stdout 输出相关
(9)sys.stderror 错误相关
进度百分比:
import time,sys
def view_bar(num,total):
rate = float(num) / float(total)
rate_num = int(rate * )
r = '\r%d%%' %(rate_num,)
sys.stdout.write(r)
sys.stdout.flush() if __name__ == "__main__":
for i in range(,):
time.sleep(0.1)
view_bar(i,)
shutil模块
高级的文件、文件夹、压缩包 处理模块
(1)copyfileobj(fsrc,fdst,length=16*1024)
def copyfileobj(fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)
shutil.copyfileobj(fsrc, fdst[, length])
import shutil #导入模块
shutil.copyfileobj(open("old_file","r"),open("new_file","w")) #打开两个文件,把一个文件的内容复制到另外一个文件
(2)shutil.copyfile(src, dst)
def copyfile(src, dst, *, follow_symlinks=True):
"""Copy data from src to dst.
If follow_symlinks is not set and src is a symbolic link, a new
symlink will be created instead of copying the file it points to.
"""
import shutil #导入文件
shutil.copyfile("old_file","new_file") #把一个文件信息导入另外一个文件
(3)shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
shutil.copymode('f1.log', 'f2.log')
(4)shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log')
(5)shutil.copy(src, dst)
拷贝文件和权限
shutil.copy('f1.log', 'f2.log')
(6)shutil.copy2(src, dst)
拷贝文件和状态信息
shutil.copy2('f1.log', 'f2.log')
(7)shutil.ignore_patterns(*patterns)
(8)shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
(9)shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
import shutil
shutil.rmtree('folder1')
(10)shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名
import shutil
shutil.move('folder1', 'folder3')
(11)shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile
#压缩 z = zipfile.ZipFile("lowb.zip","w") #首先打开文件,并向文件中添加要压缩的文件
z.write("new_file")
z.write("old_file") #向文件中添加文件,一起解压,添加压缩文件
z.close() #解压
z = zipfile.ZipFile("lowb.zip","r") #首先打开文件,然后进行解压
z.extractall() #解压文件
z.close()
tar格式的压缩和解压
import tarfile # #对文件进行压缩
# tar = tarfile.open("lowB.tar","w") #首先打开文件,添加要压缩的文件
# tar.add("new_file")
# tar.add("old_file")
# tar.close() #解压文件
tar = tarfile.open("lowB.tar","r") #首先打开文件,然后再进行解压
tar.extractall() #可设置解压地址
tar.close()
压缩解压文件都是首先要打开文件,压缩文件要向里面添加文件,添加要压缩的文件;解压文件也要先打开文件,然后使用extractall()进行解压。
day6 SYS模块的更多相关文章
- python基础--os模块和sys模块
os模块提供对操作系统进行调用的接口 # -*- coding:utf-8 -*-__author__ = 'shisanjun' import os print(os.getcwd())#获取当前工 ...
- python学习笔记(五)os、sys模块
一.os模块 print(os.name) #输出字符串指示正在使用的平台.如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'. print(os.getcwd( ...
- python之os和sys模块的区别
一.os模块 os模块是Python标准库中提供的与操作系统交互的模块,提供了访问操作系统底层的接口,里面有很多操作系统的函数 1.os常用方法 import os # print(os.getcwd ...
- python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)
1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...
- python标准模块(os及sys模块)
一.os模块 用于提供系统级别的操作 os.getcwd() 获取当前工作目录 os.stat('path/filename') 获取文件/目录信息,其中包括文件大小等 os.sep 获得操作系统特定 ...
- [转载]python中的sys模块(二)
#!/usr/bin/python # Filename: using_sys.py import sys print 'The command line arguments are:' for i ...
- [转载]Python中的sys模块
#!/usr/bin/python # Filename: cat.py import sys def readfile(filename): '''Print a file to the stand ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- sys模块和os模块,利用sys模块生成进度条
sys模块import sysprint(sys.argv)#sys.exit(0) #退出程序,正常退出exit(0)print(sys.version) #获取 ...
随机推荐
- NATS_03:NATS发布/订阅机制
概念 发布/订阅(Publish/subscribe 或pub/sub)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...
- M-JPEG、MPEG4、H.264都有何区别
压缩方式是网络视频服务器和网络摄像机的核心技术,压缩方式很大程度上决定着图像的质量.压缩比.传输效率.传输速度等性能,它是评价网络视频服务器和网络摄像机性能优劣的重要一环.随着多媒体技术的发展,相继推 ...
- Spring boot 集成 mybaits plus(代码生成器)
源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 代码生成操作 在pom.xml文件中引入以下包: <!-- mybatisplus与spr ...
- ASP.NET对无序列表批量操作的三种方法
在网页开发中,经常要用到无序列表.事实上在符合W3C标准的div+css布局中,无序列表被大量使用,ASP.NET虽然内置了BulletedList控件,用于创建和操作无序列表,但感觉不太好用.本篇介 ...
- window下卸载MySQL
更多内容推荐微信公众号,欢迎关注: 网上找来的,留在这做个备份. 1.控制面板里的增加删除程序内进行删除 2.删除MySQL文件夹下的my.ini文件,如果备份好,可以直接将文件夹全部删除 3.开始- ...
- [转]GCC常用参数详解
简介gcc and g++现在是gnu中最主要和最流行的c & c++编译器 .gcc/g++在执行编译工作的时候,总共需要以下几步:1.预处理,生成.i的文件[预处理器cpp]2.将预处理后 ...
- [转]大整数算法[11] Karatsuba乘法
★ 引子 前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) ...
- IE的双边距Bug以及解决办法
display:inline和display:block区别 一.什么是双边距Bug? 先来看图: 我们要让绿色盒模型在蓝色盒模型之内向左浮动,并且距蓝色盒模型左侧100像素.这个例子很常见,比如在网 ...
- 一个简单的爆破 mysql 远程连接脚本(perl6)
sub MAIN(Str $host) { use DBIish; my $file = open 'password.txt'; while $file.get -> $line { my $ ...
- No.4 selenium学习之路之iframe
查看iframe: 1.top window ——可以直接进行定位