Chapter VI Common module (4): python common module (shutil)

shutil module is an advanced file, folder, compressed packet processing module.

Contains copies of all forms of files and folders, delete, move, compress.

import shutil
  1. shutil.copyfileobj (fsrc, fdst [, length]) (loop) reads the contents of the file and copy the file to another, know the end of the file. The length of the string loop may be provided in each read. (No file will be created)
    python import shutil shutil.copyfileobj(open('testfile1.txt','r'), open('testfile2.txt','w'))

  2. shutil.copyfile (src, dst) Copy the file. If the file exists overwrite (no files are created)
    python import shutil shutil.copyfile('testfile1.txt', 'testfile2.txt')

  3. shutil.shutil.copymode (src, dst) permission to copy only files. Content, such as file owner information unchanged. (Dst being given does not exist)
    python import shutil shutil.copymode('testfile1.txt', 'testfile2.txt')

  4. shutil.copystat (src, dst) copying status information (not copying of contents), comprising: mode, bits, atime, mtime, flags. The file is a group (the owners) do not copy (being given dst does not exist)
    python import shutil shutil.copystat('testfile1.txt', 'testfile2.txt')

  5. shutil.copy (src, dst) to copy files and permissions. copyfile + copymode

  6. shutil.copy2 (src, dst) copy files and status information. copyfile + copystat

  7. shutil.ignore_patterns (* patterns) .copytree (src, dst, symlinks = False, ignore = None) to copy files recursively
    python import shutil shutil.copytree('testfile1.txt', 'testfile2.txt', ignore=shutil.ignore_patterns('*.pyc','tmp*')) #拷贝整个目录,并且忽略*.pyc和tmp*

  8. shutil.rmtree (path [, ignore_errors [, onerror]]) recursively delete files take

  9. shutil.move (src, dst)
    recursive moving files, and delete the specified src folder. dst folder does not exist, create

    It can be used to rename the parent folder

  10. shutil.make_archive (base_name, format, ...) to create a compressed package and return the file path (recursive compression). For example: zip, tar

    • parameter:
      • base_name: archive file name, it can be compressed path. When only passing the file name, then saved to the current directory, or saved to the specified path.
      • format: compressed packet type, 'zip', 'tar', 'bztar', 'gztar'
      • root_dir: To compress the folder path (the current directory by default)
      • owner: the user, the current user default
      • group: group, by default the current group
      • logger: for logging, usually logging.Logger objects
        `` `Python
        Import the shutil

    The '/ Users / xxx / Downloads / test' compression packing, named wwwww, stored in the current directory. Note to prevent the escape, we put in front of a string 'r'

    ret = shutil.make_archive('wwwww','gztar',root_dir=r'/Users/xxx/Downloads/test')

    The '/ Users / xxx / Downloads / test' compression packing, preservation of '/ Users / xxx / ww'

    ret = shutil.make_archive(r'/Users/xxx/ww','gztar',root_dir=r'/Users/xxx/Downloads/test')

    Use Cases keyword

    ret = shutil.make_archive(base_name='zipfile',format='zip',root_dir=r'C:/Users/kouneli/Desktop/python/temp/')

    ```

    shutil handling of calls is compressed ZipFile (compression) and tarfile (packing) to achieve two modules

Extended:

1. zipfile module: compression, decompression

zipfile module is used to compress and decompress files. 注意:zipfile不能直接递归压缩文件夹,需要遍历压缩。

  • Use Example:
    `` `Python
    Import ZipFile
    Import OS

    Compression #
    # z = zipfile.ZipFile (r'C: \ Users \ kouneli \ Desktop \ test_zip.zip ',' w ') # object with the generated compressed
    # z.write (' file1.txt ') # File pressed
    # z.close () # compress

    Compression in the recursive #
    z = zipfile.ZipFile (r'C: \ Users \ kouneli \ Desktop \ test_zip.zip ',' w ') # compress generated object
    src_dir = os.walk (r'C: \ Users \ kouneli \ Desktop \ Python \ Homework ')
    # returns (path, file within the path of the folder, files in the path), (sub-path, the file within the sub path folder, ITER files in the sub-path) by a tuple objects

    # Print (list (src_dir)) # turn into a list return value facie

    = Filelist []
    for ROOT_DIR, dirs, in src_dir Files:
    for filename in Files:
    filelist.append (the os.path.join (ROOT_DIR, filename)) # and the path name of a file stitching together (spelled pair path), then put the list.

    I in Filelist for:
    Print (I)
    z.write (I) pressed into the compressed file #

    z.close () # close the archive file

    # Decompression
    z = zipfile.ZipFile (r'C: \ Users \ kouneli \ Desktop \ test_zip.zip ',' r ') # create decompressed objects, mainly' r 'mode
    z.extractall (path = r'C: \ Users \ kouneli \ Desktop \ extra_zip ' ) # unzipped path
    z.close ()
    `` `

2. tarfile module: Packaging

Tarfile role is to document package is not compressed.

Its usage is similar zipfile, where we will not, for example.

Guess you like

Origin www.cnblogs.com/py-xiaoqiang/p/11110866.html