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 * 100)
    r = '
%d%%' %(rate_num,)
    sys.stdout.write(r)
    sys.stdout.flush()

if __name__ == "__main__":
    for i in range(0,100):
        time.sleep(0.1)
        view_bar(i,100)

    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()进行解压。

原文地址:https://www.cnblogs.com/gengcx/p/6917560.html