import shutil # copy COPY # format: shutil.copy (original path, target path), copy can rename the file, return the target path rst = shutil.copy ( ' D: \ pycharmwokspace \ demo \ demo1 ' , ' D: \ pycharmwokspace \ demo \ demoa1 ' ) # copy2 Copy # format: shutil.copy2 (original path, target path), copy is to rename the file, return the target path # copy, copy2 difference, copy2 try to keep file metadata when copying , Such as: permissions, etc. rst = shutil.copy2 ( ' D: \ pycharmwokspace \ demo \ demo2.py ' , ' D: \ pycharmwokspace \ demo \ demoa2.py ' ) # copyfile copy the contents of one file to another file # Format: shutil.copyfile (original path, target path), copy can rename the file, the return value is none rst = shutil.copyfile ( ' D: \ pycharmwokspace \ demo \ demo2.py ' , ' D: \ pycharmwokspace \ demo \ demoa2.py ' ) # move move file / folder # format: shutil.move (original path, target Path), the copy can rename the file, the return value is no # rst = shutil.move ('D: \ pycharmwokspace \ demo \ demoa2.py', 'D: \ pycharmwokspace \ demo \ demom \ demodd.py') # Archive and compression # Archive: merge multiple files or folders into one file # Compression: merge multiple files or folders lossy or lossless into one file by algorithm # make_archivearchive # format: make_archive directory and file name, '' suffix ',' need to provide documents plus ') # address after filing the return RES = shutil.make_archive ( ' D:\ pycharmwokspace \ demo \ demoaaa ', ' ZIP ' , ' demokkk ' ) Print (RES) # unpack_archive unpacked # Format: unpack_archive ( 'unpack the file path', 'address unpacked') # address returned unpacked after res = shutil.unpack_archive ( ' D: \ pycharmwokspace \ demo \ demoaaa.zip ' , ' D: \ pycharmwokspace \ demo \ demoaaa ' )
Import the ZipFile # compression # zipfile.ZipFile (File, the MODE = "r", compression = ZIP_STORED, allowZip64 = True, compresslevel = None) # create a zip objects, file represents the file path zf = zipfile.ZipFile ( ' D: \ pycharmwokspace \ Demo \ demoaaa.zip ' ) # getinfo (name) get file information info = zf.getinfo ( ' demoa2.py ' ) Print (info) # get all filenames in ZIP NL = zf.namelist () Print (NL ) # decompression extractall (self, path = None, members = None, pwd = None), path extraction path, members of all files in the default-extracting file RF = zf.extractall ( ' D: \ pycharmwokspace \ Demo') print(rf)