python basis (XVIII) - shutil module

shutil module

shutil.copyfileobj (src, dst): just copy the contents of the file, you need to open the file; create the target file does not exist, covering the presence of

shutil.copyfle (src.dst): direct copy of the file contents

shutil.copy (src, dst): Content and rights

shtuil.copy2 (src, dst): status and content information (metadata)

shutil.copymode (src, dst): Only authority; DST will exist XU

shutil.copystat (src, dst): only state; DST will exist XU

 

shutil.ignore_patterns (): globbing wildcard type

shutil.copytree (src, dst, symlink = false, ignore = none): Windows does not support symblink

>>> hutil.copytree('Tools','f2',ignore=shutil.ignore_patterns('?j.txt'))

 

shutil.rmtree (path): recursively delete files, folders will perhaps

shutil.move (src, dst): move to change the name of the current directory is equivalent to

 

shutil.make_archive(base_name,format,...)

Create a compressed package and return the file path

  • base_name: archive file name, it can be compressed path. Just when the file name is saved to the current directory, or saved to a specified location, such as: www => to save the current path, such as: / Users / wupeiqi / www => Save to / Users / wupeiqi /
  • 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, the object is usually logging.Logger
import shutil 
a = shutil.make_archive (r ' . \ test2 ' , ' zip ' , r ' . \ test1 ' )

 

About zipfile and tarfile two modules

Import ZipFile, tarfile
 # compressed 
Z = zipfile.ZipFile ( ' lala.zip ' , ' W ' ) 
z.write ( ' test1 ' ) 
z.close () 

T = tarfile.open ( ' haha.zip ' , ' W ' ) 
t.add ( ' test1 ' , ' test1-1 ' ) # test1-1 name is compressed 
t.close ()
 # decompression 
z_decom = zipfile.ZipFile ( ' lala.zip ' , 'R & lt ' ) 
z_decom.extract ( ' test1 / ' ) # specify file, Windows folder needs / expressed 
z_decom.close () 

t_decom = tarfile.open ( ' haha.zip ' , ' R & lt ' ) 
t_decom.extractall ()     # extractall not need to specify mem, you can specify to extract the path 
_decom.close ()

 

Each file path can not be compressed inside the compression

With os.walk () to extract it, the path to each file can achieve this requirement

 Detailed usage https://www.cnblogs.com/ManyQian/p/9193199.html two modules

Guess you like

Origin www.cnblogs.com/lalaxing/p/11369643.html