defcopyfile(src, dst, *, follow_symlinks=True): """尝试用最快的方式复制文件. 如果 follow_symlinks 为假值且 src 为符号链接,则将创建一个新的符号链接而不是拷贝 src 所指向的文件 (If follow_symlinks is not set and src is a symbolic link, a new symlink will be created instead of copying the file it points to.) """ # 如果 src 和 dst 指定了同一个文件,则将引发 SameFileError。 if _samefile(src, dst): # 见注2.1 raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
file_size = 0 for i, fn inenumerate([src, dst]): try: # 注2.4 st = _stat(fn) except OSError: # File most likely does not exist pass else: # XXX What about other special files? (sockets, devices...) if stat.S_ISFIFO(st.st_mode): fn = fn.path ifisinstance(fn, os.DirEntry) else fn raise SpecialFileError("`%s` is a named pipe" % fn) if _WINDOWS and i == 0: file_size = st.st_size
"First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'.
"Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first
def_copyfileobj_readinto(fsrc, fdst, length=COPY_BUFSIZE): """ 基于 readinto()/memoryview() 的 copyfileobj() 的变体. readinto()/memoryview() based variant of copyfileobj(). *fsrc* must support readinto() method and both files must be open in binary mode. """ # Localize variable access to minimize overhead. fsrc_readinto = fsrc.readinto fdst_write = fdst.write withmemoryview(bytearray(length)) as mv: whileTrue: n = fsrc_readinto(mv) ifnot n: break elif n < length: with mv[:n] as smv: fdst.write(smv) else: fdst_write(mv)