Shutil file for the Python standard library

Source: Internet
Author: User

Import Shutil,glob,os
#作用: Handles some file operations, such as copying, setting permissions
The #复制文件copyfile () copies the source content to the target and generates IOERROR if no permission is written to the destination file
print ' Before: ', Glob.glob (' *.txt ')
Shutil.copyfile (' Lorem.txt ', R ' Copy/lorem.txt ')
print ' After: ', Glob.glob (R ' Copy/lorem.txt ')
#由于这个函数会打开输入文件进行读取, regardless of their type, so some special files (such as UNIX device nodes) cannot be copied to a new special file using CopyFile ().

The #copyfile () implementation uses the underlying function copyfileobj (), the CopyFile () parameter is the file name, but the copyfileobj () parameter is the open file handle. You can also have a third parameter (optional): A buffer length for reading into a block
From Stringio import Stringio as Io
Import Sys,time
Class Verbose (IO):
def read (self, n =-1):
Next1=io.read (Self,n)
print ' read (%d) bytes '% (n)
Return NEXT1
L= "The codecs module provides stream and file" \
"Interfaces for transcoding data in your. It is the most "\
"Commonly used to work with Unicode text, and other encodings" \
"is also available for other purposes."
print ' Default: ',
Input1=verbose (L)
Ouput1=io ()
Shutil.copyfileobj (INPUT1,OUPUT1)
Print
print ' All at once: '
Input1=verbose (L)
Ouput1=io ()
Shutil.copyfileobj (input1,ouput1,-1)
Print
print ' Blocks of 256: '
Input1=verbose (L)
Ouput1=io ()
Shutil.copyfileobj (input1,ouput1,256)
Print
#默认行为是使用大数据读取, use 1 to read all at once, or use a different positive number to set a specific block size

The #类似于unix命令行工具 (Cp,copy () function) interprets the output name in the same way, and if the specified target indicates a directory instead of a file, a new file is created in the directory using the source file base name.
Os.mkdir (R ' copy/abc ')
print ' Beform: ', Os.listdir (R ' copy/abc ')
Shutil.copy (R ' Lorem.txt ', R ' Copy/abc ')
print ' After: ', Os.listdir (R ' copy/abc ')
#copy2 () works like copy (), but the metadata that is copied to the new file contains access and modification data
def show_file_info (filename):
Stat_info=os.stat (filename)
print ' \tmode: ', Stat_info.st_mode
print ' tcreated: ', Time.ctime (stat_info.st_ctime)
print ' \ t accessed: ', Time.ctime (stat_info.st_atime)
print ' \tmodified: ', Time.ctime (stat_info.st_mtime)

Os.mkdir (R ' COPY/ABCDE ')
print ' Source: '
Show_file_info (' Lorem.txt ')
Shutil.copy2 (' Lorem.txt ', R ' Copy/abcde ')
print ' dest: '
Show_file_info (' Copy/abcde/lorem.txt ')
#这个新文件所有特性都与原文件完全相同

#复制文件元数据
#默认地, when creating a new file under UNIX, it will accept permissions based on the current user's umask, to copy permissions from one file to another, using Copymode ()
From commands Import *
With open (R ' lorem.txt ', ' wt ') as F:
F.write (' content ')
Os.chmod (R ' Lorem.txt ', 0444)
print ' Before: '
Print GetStatus (R ' Lorem.txt ')
Shutil.copymode (R ' Lorem.txt ', R ' Copy/lorem.doc ')
print ' After: '
Print GetStatus (R ' Copy/lorem.doc ')
#要复制文件的其他元数据, you can use Copystat ()
def show_file_info (filename):
Stat_info=os.stat (filename)
print ' \tmode: ', Stat_info.st_mode
print ' tcreated: ', Time.ctime (stat_info.st_ctime)
print ' \ t accessed: ', Time.ctime (stat_info.st_atime)
print ' \tmodified: ', Time.ctime (stat_info.st_mtime)
With open (R ' lorem.txt ', ' wt ') as F:
F.write (' content ')
Os.chmod (R ' Lorem.txt ', 0444)
print ' Before: '
Print GetStatus (R ' Lorem.txt ')
Shutil.copystat (R ' Lorem.txt ', R ' Copy/lorem.doc ')
print ' After: '
#使用copystat () copies only the permissions and dates associated with the file


#处理目录树干
"""
Shutil contains 3 functions to handle a directory tree, to copy from one directory to another, you can use Copytree (), which recursively traverses the source tree, copies the file to the destination, and the destination directory cannot already exist.
Note: the Copytree () document states that it should be seen as an example implementation, rather than as a tool, to consider the current implementation as a starting point,
Make it more robust before you actually use it, or you can add features such as a progress bar!
"""
print ' Befort: '
Print getoutput (r ' copy ')
Shutil.copytree (R ' copy ', R ' Copy/abcde ')
print ' After: '
Print getoutput (r ' copy ')

#symlinks参数控制着符号作为链接复制还是作为文件复制, the content is copied to the new file by default, and if this option is true, a new symbolic link is created in the target tree

#要删除一个目录及其中内容, you can use Rmtree ()
print ' Befort: '
Print getoutput (r ' copy ')
Shutil.rmtree (R ' copy ', R ' Copy/abcde ')
print ' After: '
Print getoutput (r ' copy ')
#默认地, errors are generated as exceptions, but if the second argument is true, you can ignore the exceptions and provide a special error-handling function in the third parameter
#要把一个文件或者目录从一个位置移动到另一个位置可以使用move.
With open (' aa.txt ', ' wt ') as F:
F.write (' AA ')
print ' Beform: ', Glob.glob (' CC ')
Shutil.move (' Aa.txt ', ' Aa.bim ')
print ' Atfer: ', Glob.glob (' CC ')

#其语义与unix命令mv类似, if both the source and destination are in the same file system, the file will be renamed, otherwise the source file will be copied to the destination file and the source file will be deleted!
#shutil官方地址: https://docs.python.org/2/library/shutil.html

Shutil file for the Python standard library

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.