Examples of common file operation function usages for shutil modules in Python _python

Source: Internet
Author: User
Tags chmod file copy lstat parent directory symlink file permissions in python

The OS module provides new/delete/view file properties for directories or files, and also provides path actions for files and directories.  For example: absolute path, parent directory ... However, OS file operations should also include operations such as mobile replication package compression decompression, which are not provided by the OS modules.
The shutil in this article is the addition of file operations in the OS. --Mobile Replication package compression decompression,
Shutil function Function:
1 shutil.copyfileobj (FSRC, fdst[, length=16*1024])
Copy file contents to another file, you can copy the content of the specified size
Let's take a look at the source code.

def copyfileobj (FSRC, FDST, length=16*1024): "" "
  copy data from File-like object FSRC to File-like object Fdst" "
  W Hile 1:
    buf = fsrc.read (length)
    if not buf:
      break
    fdst.write (BUF)

Attention! In which fsrc,fdst are file objects that need to be opened before replication can take place

Import shutil
f1=open (' name ', ' R ')
F2=open (' name_copy ', ' w+ ')
shutil.copyfileobj (f1,f2,length=16* 1024)


2 Shutil.copyfile (SRC,DST)
Copy file content, is not feeling the above file copy very troublesome? You also need to use the Open function in your own hand, you don't need it here, in fact, CopyFile called the Copyfileobj

def copyfile (SRC, DST, *, follow_symlinks=true):
  if _samefile (SRC, DST):
    raise Samefileerror ("{!r} and {!r} are T He same file ". Format (src, DST)) for
  fn in [SRC, DST]:
    try:
      st = Os.stat (FN)
    except OSError:
      # File MoS T likely does not exist
      pass
    else:
      # XXX What about other special files? (Sockets, devices ...)
      If Stat. S_isfifo (St.st_mode):
        Raise Specialfileerror ("'%s ' is a named pipe"% fn)
  if not follow_symlinks and os.path.is Link (src):
    os.symlink (Os.readlink (SRC), DST)
  else:
    with open (SRC, ' RB ') as FSRC:
      with open (DST, ' WB ') as FDST:
        copyfileobj (FSRC, FDST) return
  DST
Shutil.copyfile (' name ', ' name_copy_2 ')
#一句就可以实现复制文件内容

3 Shutil.copymode (SRC,DST)
copy-only permissions, do not change file contents, groups, and users.

def copymode (SRC, DST, *, follow_symlinks=true):
  if not follow_symlinks and Os.path.islink (SRC) and Os.path.islink ( DST):
    if hasattr (OS, ' Lchmod '):
      stat_func, Chmod_func = Os.lstat, os.lchmod
    else:
      return
  elif Hasattr (OS, ' chmod '):
    stat_func, Chmod_func = Os.stat, os.chmod
  else:
    return
  st = Stat_func (SRC)
  Chmod_func (DST, Stat. S_imode (St.st_mode))

Read two file permissions first

[Root@slyoyo python_test]# ls-l Total
4
-rw-r--r--. 1 root-May-05:17 test1
. 1 root R Oot 0 May 19:10 test2

Run command

>>> import shutil
>>> shutil.copymode (' test1 ', ' test2 ')

View Results

[Root@slyoyo python_test]# ls-l Total
4
-rw-r--r--. 1 root
--. 1 05:17 test1-rw-r--r Root 0 May 19:10 Test2

When we change the target file to a nonexistent file The Times is wrong

>>> shutil.copymode (' test1 ', ' test3 ')  
Traceback (most recent call last):
 File "<stdin>", line 1, In <module>
 File "/usr/local/python/lib/python3.4/shutil.py", line 132, in Copymode
  Chmod_func (DST, Stat. S_imode (St.st_mode))
filenotfounderror: [Errno 2] No such file or directory: ' test233 '

4 Shutil.copystat (SRC,DST)
Copy all status information, including permissions, group, user, time, etc.

def copystat (SRC, DST, *, follow_symlinks=true): Def _nop (*args, Ns=none, Follow_symlinks=none): Pass # Follow S
  Ymlinks (aka Don ' t follow symlinks) follow = Follow_symlinks or not (Os.path.islink (SRC) and Os.path.islink (DST))
    If follow: # Use the real function if it exists def lookup (name): Return getattr (OS, name, _NOP) Else: # Use the ' real ' function only if it exists # *and* it supports follow_symlinks def lookup (name): fn = Getatt R (OS, name, _NOP) if FN in Os.supports_follow_symlinks:return fn return _nop st = lookup ("stat") (SR C, follow_symlinks=follow) mode = Stat. S_imode (St.st_mode) lookup ("Utime") (DST, ns= (St.st_atime_ns, St.st_mtime_ns), Follow_symlinks=follow) Try:loo Kup ("chmod") (DST, mode, follow_symlinks=follow) except Notimplementederror: # If we got a notimplementederror, it ' s Because # * Follow_symlinks=false, # * Lchown () is unavailable, and # * either # *Fchownat () is unavailable or # * FCHOWNAT () doesn ' t implement At_symlink_nofollow.
    # (it returned enosup.) # therefore we ' re out of Options--we simply cannot chown the # symlink.
    Give up, suppress the error.
    # (which is what shutil always did to this circumstance.) Pass if Hasattr (St, ' St_flags '): Try:lookup ("Chflags") (DST, St.st_flags, follow_symlinks=follow) except O Serror as Why:for err in ' Eopnotsupp ', ' enotsup ': If Hasattr (errno, err) and Why.errno = = GetAttr (errno, er

 R): Break Else:raise _copyxattr (SRC, DST, Follow_symlinks=follow)


5 shutil.copy (SRC,DST)
Copy the contents and permissions of the file, first copyfile after Copymode

 def copy (SRC, DST, *, follow_symlinks=true):

  if Os.path.isdir (DST):
    DST = os.path.join (DST, Os.path.basename ( SRC))
  copyfile (src, DST, follow_symlinks=follow_symlinks)
  copymode (src, DST, follow_symlinks=follow_ symlinks) return
  DST


6 shutil.copy2 (SRC,DST)
copies the contents of the file and all state information for the file. First CopyFile, then Copystat.

def copy2 (SRC, DST, *, follow_symlinks=true): "" "
  Copy data and all stat info (" cp-p src DST "). Return
  the
  file ' s destination. The destination may is a directory.
  If Follow_symlinks is False, the symlinks won ' t be followed. This
  resembles GNU "cp-p src DST".
  "" " If Os.path.isdir (DST):
    DST = os.path.join (DST, Os.path.basename (SRC))
  copyfile (src, DST, follow_symlinks= Follow_symlinks)
  copystat (src, DST, follow_symlinks=follow_symlinks) return
  DST


7 shutil.copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2,ignore_dangling_symlinks=false)
Recursive copy of file contents and status information

def copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2, ignore_dangling_symlinks=false): Names = Os.listdir (src) If ignore is not none:ignored_names = Ignore (src, names) Else:ignored_names = set () OS. Makedirs (DST) errors = [] for name in Names:if name in ignored_names:continue SrcName = Os.path.join (s RC, name) DstName = Os.path.join (DST, name) try:if Os.path.islink (srcname): Linkto = Os.readlink (sr CNAME) if symlinks: # We can ' t just leave it to ' copy_function ' because legacy # code with a C
          Ustom ' copy_function ' may rely in Copytree # doing the right thing. Os.symlink (Linkto, DstName) copystat (SrcName, DstName, Follow_symlinks=not symlinks) Else: # I
            Gnore dangling Symlink If the flag is in if not os.path.exists (Linkto) and ignore_dangling_symlinks: Continue # Otherwise let the copy occurs. Copy2 Would raise an error if Os.path.isdir (srcname): Copytree (SrcName, DstName, symlinks, ignore,
        copy_function) else:copy_function (SrcName, DstName) elif Os.path.isdir (SrcName): Copytree (SrcName, DstName, symlinks, ignore, copy_function) Else: # would raise a specialfileerror for Unsupported file Types Copy_function (SrcName, DstName) # Catch the Error from the recursive copytree 
      E can # Continue with the other files except Error as Err:errors.extend (Err.args[0]) except OSError as why: Errors.append ((SrcName, DstName, str (why)) Try:copystat (SRC, DST) except OSError as why: # copying FI Le access times may fail to Windows if GetAttr (why, ' Winerror ', None) is None:errors.append (SRC, DST, str (why)

 ) If Errors:raise Error (Errors) return DST # version vulnerable to race conditions
[Root@slyoyo python_test]# tree copytree_test/
copytree_test/
└──test
  ├──test1
  ├──test2
  └── hahaha
[Root@slyoyo test]# ls-l total
0
-rw-r--r--. 1 python python 0 may 19:36 hahaha-rw-r--r
--. 1 p Ython python 0 may 19:36 test1
-rw-r--r--. 1 root  root 0 may  19:36 test2
>>> Shutil.cop Ytree (' copytree_test ', ' copytree_copy ')
' copytree_copy '
[Root@slyoyo python_test]# Total
12
Drwxr-xr-x 3 root  root  4096 may 19:36 copytree_copy
drwxr-xr-x. 3 root root  4096 may 19:36 copytree_test
-rw-r--r--. 1 python python-  05:17 test1-rw-r--r
--. 1 root  root
   
    0 19:10 test2
[Root@slyoyo python_test]# tree copytree_copy/copytree_copy/└──test-├──
  hahaha
  ├──test1
  └──test2

   


8 Shutil.rmtree (Path, Ignore_errors=false, Onerror=none)
to delete a file recursively

def rmtree (Path, Ignore_errors=false, Onerror=none): If Ignore_errors:def onerror (*args): Pass Elif Onerr  None:def onerror (*args): Raise if _use_fd_functions: # While the unsafe Rmtree works fine on bytes,
    The FD based does not. 
    If isinstance (path, bytes): Path = Os.fsdecode (path) # Note:to guard against Symlink races, we use the standard
    # Lstat ()/open ()/fstat () trick. Try:orig_st = os.lstat (path) except Exception:onerror (Os.lstat, Path, Sys.exc_info ()) return T RY:FD = Os.open (path, OS. o_rdonly) except Exception:onerror (Os.lstat, Path, Sys.exc_info ()) return try:if Os.path.sames Tat (Orig_st, Os.fstat (FD)): _rmtree_safe_fd (FD, Path, onerror) try:os.rmdir (path) excep T Oserror:onerror (os.rmdir, Path, Sys.exc_info ()) Else:try: # symlinks to Directories are forbidden, the bug #1669 raiseOSError ("Cannot call Rmtree on a symbolic link") except Oserror:onerror (Os.path.islink, Path, sys.exc_i
 NFO ()) Finally:os.close (FD) Else:return _rmtree_unsafe (path, onerror)


9 Shutil.move (SRC, DST)
Recursive moving files

def move (SRC, DST):

  REAL_DST = DST
  if Os.path.isdir (DST):
    if _samefile (SRC, DST):
      # We might being on a case Insensitive filesystem,
      # Perform the rename anyway.
      Os.rename (SRC, DST)
      return
    REAL_DST = Os.path.join (DST, _basename (SRC))
    if Os.path.exists (REAL_DST):
      Raise Error ("Destination path '%s ' already exists"% REAL_DST)
  try:
    os.rename (SRC, real_dst)
  Except OSError:
    if Os.path.islink (src):
      linkto = Os.readlink (src)
      os.symlink (Linkto, REAL_DST)
      Os.unlink (SRC)
    elif os.path.isdir (src):
      if _DESTINSRC (SRC, DST):
        raise Error ("Cannot move a directory" %s ' into itself '%s '% (src, DST))
      copytree (SRC, REAL_DST, symlinks=true)
      rmtree (SRC)
    else:
      Copy2 (SRC, real_dst)
      os.unlink (SRC) return
  REAL_DST


Make_archive (base_name, format, Root_dir=none, Base_dir=none, verbose=0,dry_run=0, Owner=none, Group=None, logger= None)

Compress packaging

 def make_archive (base_name, format, Root_dir=none, Base_dir=none, verbose=0, Dry_run=0, O
      Wner=none, Group=none, logger=none): SAVE_CWD = OS.GETCWD () If Root_dir is not none:if logger are not None: Logger.debug ("Changing into '%s '", root_dir) Base_name = Os.path.abspath (base_name) if not Dry_run:os.chdi R (Root_dir) if base_dir is None:base_dir = Os.curdir Kwargs = {' Dry_run ': dry_run, ' logger ': logger} try:f Ormat_info = _archive_formats[format] except keyerror:raise valueerror ("Unknown ARCHIVE format '%s '% format) Fu NC = format_info[0] for Arg, Val in format_info[1]: kwargs[arg] = val If format!= ' Zip ': kwargs[' owner ' = ow NER kwargs[' group ' = Group Try:filename = func (Base_name, Base_dir, **kwargs) finally:if Root_dir is no T none:if logger is isn't None:logger.debug ("Changing Back to '%s '", SAVE_CWD) Os.chdir (SAVE_CWD) re Turn filename 


Base_name: Compress the packaged file name or path name
Format: Compress or package formats "zip", "tar", "Bztar" or "Gztar"
Root_dir: Which directory or file to package (i.e. source file)

>>> shutil.make_archive (' tarball ', ' Gztar ', root_dir= ' copytree_test ')
[Root@slyoyo python_test]# Total
drwxr-xr-x 3 root root  4096 may  19:36 copytree_copy
drwxr-xr-x. 3 root  root 4096 May 19:36 copytree_test
-rw-r--r--. 1 root 0 could 21:12 tarball.tar.gz-rw-r--r
--. 1 PYT Hon Python  05:17 test1
-rw-r--r--. 1 root  root   0 may 19:10 test2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.