os模块中的shutil的使用方式与方法
生活随笔
收集整理的這篇文章主要介紹了
os模块中的shutil的使用方式与方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#os 模塊os.sep 可以取代操作系統(tǒng)特定的路徑分隔符。windows下為 '\\'
os.name 字符串指示你正在使用的平臺。比如對于Windows,它是'nt',而對于Linux/Unix用戶,它是 'posix'
os.getcwd() 函數(shù)得到當(dāng)前工作目錄,即當(dāng)前Python腳本工作的目錄路徑
os.getenv() 獲取一個環(huán)境變量,如果沒有返回none
os.putenv(key, value) 設(shè)置一個環(huán)境變量值
os.listdir(path) 返回指定目錄下的所有文件和目錄名
os.remove(path) 函數(shù)用來刪除一個文件
os.system(command) 函數(shù)用來運(yùn)行shell命令
os.linesep 字符串給出當(dāng)前平臺使用的行終止符。例如,Windows使用 '\r\n',Linux使用 '\n' 而Mac使用 '\r'
os.path.split(path) 函數(shù)返回一個路徑的目錄名和文件名
os.path.isfile() 和os.path.isdir()函數(shù)分別檢驗(yàn)給出的路徑是一個文件還是目錄
os.path.exists() 函數(shù)用來檢驗(yàn)給出的路徑是否真地存在
os.curdir 返回當(dāng)前目錄 ('.')
os.mkdir(path) 創(chuàng)建一個目錄
os.makedirs(path) 遞歸的創(chuàng)建目錄
os.chdir(dirname) 改變工作目錄到dirname
os.path.getsize(name) 獲得文件大小,如果name是目錄返回0L
os.path.abspath(name) 獲得絕對路徑
os.path.normpath(path) 規(guī)范path字符串形式
os.path.splitext() 分離文件名與擴(kuò)展名
os.path.join(path,name) 連接目錄與文件名或目錄
os.path.basename(path) 返回文件名
os.path.dirname(path) 返回文件路徑
os.walk(top,topdown=True,οnerrοr=None) 遍歷迭代目錄
os.rename(src, dst) 重命名file或者directory src到dst 如果dst是一個存在的directory, 將拋出OSError. 在Unix, 如果dst在存且是一個file, 如果用戶有權(quán)限的話,它將被安靜的替換. 操作將會失敗在某些Unix 中如果src和dst在不同的文件系統(tǒng)中. 如果成功, 這命名操作將會是一個原子操作 (這是POSIX 需要). 在 Windows上, 如果dst已經(jīng)存在, 將拋出OSError,即使它是一個文件. 在unix,Windows中有效。
os.renames(old, new) 遞歸重命名文件夾或者文件。像rename()# shutil 模塊
shutil.copyfile( src, dst) 從源src復(fù)制到dst中去。當(dāng)然前提是目標(biāo)地址是具備可寫權(quán)限。拋出的異常信息為IOException. 如果當(dāng)前的dst已存在的話就會被覆蓋掉
shutil.move( src, dst) 移動文件或重命名
shutil.copymode( src, dst) 只是會復(fù)制其權(quán)限其他的東西是不會被復(fù)制的
shutil.copystat( src, dst) 復(fù)制權(quán)限、最后訪問時間、最后修改時間
shutil.copy( src, dst) 復(fù)制一個文件到一個文件或一個目錄
shutil.copy2( src, dst) 在copy上的基礎(chǔ)上再復(fù)制文件最后訪問時間與修改時間也復(fù)制過來了,類似于cp –p的東西
shutil.copy2( src, dst) 如果兩個位置的文件系統(tǒng)是一樣的話相當(dāng)于是rename操作,只是改名;如果是不在相同的文件系統(tǒng)的話就是做move操作
shutil.copytree( olddir, newdir, True/Flase)
把olddir拷貝一份newdir,如果第3個參數(shù)是True,則復(fù)制目錄時將保持文件夾下的符號連接,如果第3個參數(shù)是False,則將在復(fù)制的目錄下生成物理副本來替代符號連接
shutil.rmtree( src ) 遞歸刪除一個目錄以及目錄內(nèi)的所有內(nèi)容NAME
??? shutil - Utility functions for copying and archiving files and directory trees.
FILE
??? /usr/lib/python2.7/shutil.py
MODULE DOCS
??? http://docs.python.org/library/shutil
DESCRIPTION
??? XXX The functions here don't copy the resource fork or other metadata on Mac.
CLASSES
??? exceptions.EnvironmentError(exceptions.StandardError)
??????? Error
??????? ExecError
??????? SpecialFileError
?? ?
??? class Error(exceptions.EnvironmentError)
???? |? Method resolution order:
???? |????? Error
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
?|? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
?| ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
?? ?
??? class ExecError(exceptions.EnvironmentError)
???? |? Raised when a command could not be executed
???? | ?
???? |? Method resolution order:
???? |????? ExecError
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
???? | ?
???? |? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
? |? ----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
???? | ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
?class SpecialFileError(exceptions.EnvironmentError)
???? |? Raised when trying to do a kind of operation (e.g. copying) which is
???? |? not supported on a special file (e.g. a named pipe)
???? | ?
???? |? Method resolution order:
???? |????? SpecialFileError
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
???? | ?
???? |? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
?----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
???? | ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
FUNCTIONS
??? copy(src, dst)
??????? Copy data and mode bits ("cp src dst").
?????? ?
??????? The destination may be a directory.
copyfile(src, dst)
??????? Copy data from src to dst
?? ?
??? copyfileobj(fsrc, fdst, length=16384)
??????? copy data from file-like object fsrc to file-like object fdst
?? ?
??? copymode(src, dst)
??????? Copy mode bits from src to dst
?? ?
??? copystat(src, dst)
??????? Copy all stat info (mode bits, atime, mtime, flags) from src to dst
?? ?
??? copytree(src, dst, symlinks=False, ignore=None)
??????? Recursively copy a directory tree using copy2().
?????? ?
??????? The destination directory must not already exist.
??????? If exception(s) occur, an Error is raised with a list of reasons.
?????? ?
??????? If the optional symlinks flag is true, symbolic links in the
??????? source tree result in symbolic links in the destination tree; if
??????? it is false, the contents of the files pointed to by symbolic
??????? links are copied.
?????? ?
??????? The optional ignore argument is a callable. If given, it
??????? is called with the `src` parameter, which is the directory
??????? being visited by copytree(), and `names` which is the list of
??????? `src` contents, as returned by os.listdir():
?????? ?
??????????? callable(src, names) -> ignored_names
?????? ?
??????? Since copytree() is called recursively, the callable will be
??????? called once for each directory that is copied. It returns a
??????? list of names relative to the `src` directory that should
??????? not be copied.
?????? ?
??????? XXX Consider this example code rather than the ultimate tool.
?? ?
??? get_archive_formats()
??????? Returns a list of supported formats for archiving and unarchiving.
?????? ?
??????? Each element of the returned sequence is a tuple (name, description)
?? ?
??? ignore_patterns(*patterns)
Function that can be used as copytree() ignore parameter.
?????? ?
??????? Patterns is a sequence of glob-style patterns
??????? that are used to exclude files
?? ?
??? make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
??????? Create an archive file (eg. zip or tar).
?????? ?
??????? 'base_name' is the name of the file to create, minus any format-specific
??????? extension; 'format' is the archive format: one of "zip", "tar", "bztar"
??????? or "gztar".
?????? ?
??????? 'root_dir' is a directory that will be the root directory of the
??????? archive; ie. we typically chdir into 'root_dir' before creating the
??????? archive.? 'base_dir' is the directory where we start archiving from;
??????? ie. 'base_dir' will be the common prefix of all files and
??????? directories in the archive.? 'root_dir' and 'base_dir' both default
??????? to the current directory.? Returns the name of the archive file.
?????? ?
??????? 'owner' and 'group' are used when creating a tar archive. By default,
??????? uses the current owner and group.
?? ?
??? move(src, dst)
??????? Recursively move a file or directory to another location. This is
??????? similar to the Unix "mv" command.
?????? ?
??????? If the destination is a directory or a symlink to a directory, the source
??????? is moved inside the directory. The destination path must not already
??????? exist.
?????? ?
??????? If the destination already exists but is not a directory, it may be
??????? overwritten depending on os.rename() semantics.
?????? ?
??????? If the destination is on our current filesystem, then rename() is used.
??????? Otherwise, src is copied to the destination and then removed.
??????? A lot more could be done here...? A look at a mv.c shows a lot of
??????? the issues this implementation glosses over.
?? ?
??? register_archive_format(name, function, extra_args=None, description='')
??????? Registers an archive format.
?????? ?
??????? name is the name of the format. function is the callable that will be
??????? used to create archives. If provided, extra_args is a sequence of
??????? (name, value) tuples that will be passed as arguments to the callable.
??????? description can be provided to describe the format, and will be returned
??????? by the get_archive_formats() function.
rmtree(path, ignore_errors=False, οnerrοr=None)
??????? Recursively delete a directory tree.
?????? ?
??????? If ignore_errors is set, errors are ignored; otherwise, if onerror
??????? is set, it is called to handle the error with arguments (func,
??????? path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
??????? path is the argument to that function that caused it to fail; and
??????? exc_info is a tuple returned by sys.exc_info().? If ignore_errors
??????? is false and onerror is None, an exception is raised.
?? ?
??? unregister_archive_format(name)
DATA
??? __all__ = ['copyfileobj', 'copyfile', 'copymode', 'copystat', 'copy', ...
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
??? shutil - Utility functions for copying and archiving files and directory trees.
FILE
??? /usr/lib/python2.7/shutil.py
MODULE DOCS
??? http://docs.python.org/library/shutil
DESCRIPTION
??? XXX The functions here don't copy the resource fork or other metadata on Mac.
CLASSES
??? exceptions.EnvironmentError(exceptions.StandardError)
??????? Error
??????? ExecError
??????? SpecialFileError
?? ?
??? class Error(exceptions.EnvironmentError)
???? |? Method resolution order:
???? |????? Error
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
?|? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
?| ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
?? ?
??? class ExecError(exceptions.EnvironmentError)
???? |? Raised when a command could not be executed
???? | ?
???? |? Method resolution order:
???? |????? ExecError
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
???? | ?
???? |? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
? |? ----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
???? | ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
?class SpecialFileError(exceptions.EnvironmentError)
???? |? Raised when trying to do a kind of operation (e.g. copying) which is
???? |? not supported on a special file (e.g. a named pipe)
???? | ?
???? |? Method resolution order:
???? |????? SpecialFileError
???? |????? exceptions.EnvironmentError
???? |????? exceptions.StandardError
???? |????? exceptions.Exception
???? |????? exceptions.BaseException
???? |????? __builtin__.object
???? | ?
???? |? Data descriptors defined here:
???? | ?
???? |? __weakref__
???? |????? list of weak references to the object (if defined)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.EnvironmentError:
???? | ?
???? |? __init__(...)
???? |????? x.__init__(...) initializes x; see help(type(x)) for signature
???? | ?
???? |? __reduce__(...)
???? | ?
???? |? __str__(...)
???? |????? x.__str__() <==> str(x)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.EnvironmentError:
???? | ?
???? |? errno
???? |????? exception errno
???? | ?
???? |? filename
???? |????? exception filename
???? | ?
???? |? strerror
???? |????? exception strerror
?----------------------------------------------------------------------
???? |? Data and other attributes inherited from exceptions.EnvironmentError:
???? | ?
???? |? __new__ = <built-in method __new__ of type object>
???? |????? T.__new__(S, ...) -> a new object with type S, a subtype of T
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Methods inherited from exceptions.BaseException:
???? | ?
???? |? __delattr__(...)
???? |????? x.__delattr__('name') <==> del x.name
???? | ?
???? |? __getattribute__(...)
???? |????? x.__getattribute__('name') <==> x.name
???? | ?
???? |? __getitem__(...)
???? |????? x.__getitem__(y) <==> x[y]
???? | ?
???? |? __getslice__(...)
???? |????? x.__getslice__(i, j) <==> x[i:j]
???? |???? ?
???? |????? Use of negative indices is not supported.
???? | ?
???? |? __repr__(...)
???? |????? x.__repr__() <==> repr(x)
???? | ?
???? |? __setattr__(...)
???? |????? x.__setattr__('name', value) <==> x.name = value
???? | ?
???? |? __setstate__(...)
???? | ?
???? |? __unicode__(...)
???? | ?
???? |? ----------------------------------------------------------------------
???? |? Data descriptors inherited from exceptions.BaseException:
???? | ?
???? |? __dict__
???? | ?
???? |? args
???? | ?
???? |? message
FUNCTIONS
??? copy(src, dst)
??????? Copy data and mode bits ("cp src dst").
?????? ?
??????? The destination may be a directory.
copyfile(src, dst)
??????? Copy data from src to dst
?? ?
??? copyfileobj(fsrc, fdst, length=16384)
??????? copy data from file-like object fsrc to file-like object fdst
?? ?
??? copymode(src, dst)
??????? Copy mode bits from src to dst
?? ?
??? copystat(src, dst)
??????? Copy all stat info (mode bits, atime, mtime, flags) from src to dst
?? ?
??? copytree(src, dst, symlinks=False, ignore=None)
??????? Recursively copy a directory tree using copy2().
?????? ?
??????? The destination directory must not already exist.
??????? If exception(s) occur, an Error is raised with a list of reasons.
?????? ?
??????? If the optional symlinks flag is true, symbolic links in the
??????? source tree result in symbolic links in the destination tree; if
??????? it is false, the contents of the files pointed to by symbolic
??????? links are copied.
?????? ?
??????? The optional ignore argument is a callable. If given, it
??????? is called with the `src` parameter, which is the directory
??????? being visited by copytree(), and `names` which is the list of
??????? `src` contents, as returned by os.listdir():
?????? ?
??????????? callable(src, names) -> ignored_names
?????? ?
??????? Since copytree() is called recursively, the callable will be
??????? called once for each directory that is copied. It returns a
??????? list of names relative to the `src` directory that should
??????? not be copied.
?????? ?
??????? XXX Consider this example code rather than the ultimate tool.
?? ?
??? get_archive_formats()
??????? Returns a list of supported formats for archiving and unarchiving.
?????? ?
??????? Each element of the returned sequence is a tuple (name, description)
?? ?
??? ignore_patterns(*patterns)
Function that can be used as copytree() ignore parameter.
?????? ?
??????? Patterns is a sequence of glob-style patterns
??????? that are used to exclude files
?? ?
??? make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
??????? Create an archive file (eg. zip or tar).
?????? ?
??????? 'base_name' is the name of the file to create, minus any format-specific
??????? extension; 'format' is the archive format: one of "zip", "tar", "bztar"
??????? or "gztar".
?????? ?
??????? 'root_dir' is a directory that will be the root directory of the
??????? archive; ie. we typically chdir into 'root_dir' before creating the
??????? archive.? 'base_dir' is the directory where we start archiving from;
??????? ie. 'base_dir' will be the common prefix of all files and
??????? directories in the archive.? 'root_dir' and 'base_dir' both default
??????? to the current directory.? Returns the name of the archive file.
?????? ?
??????? 'owner' and 'group' are used when creating a tar archive. By default,
??????? uses the current owner and group.
?? ?
??? move(src, dst)
??????? Recursively move a file or directory to another location. This is
??????? similar to the Unix "mv" command.
?????? ?
??????? If the destination is a directory or a symlink to a directory, the source
??????? is moved inside the directory. The destination path must not already
??????? exist.
?????? ?
??????? If the destination already exists but is not a directory, it may be
??????? overwritten depending on os.rename() semantics.
?????? ?
??????? If the destination is on our current filesystem, then rename() is used.
??????? Otherwise, src is copied to the destination and then removed.
??????? A lot more could be done here...? A look at a mv.c shows a lot of
??????? the issues this implementation glosses over.
?? ?
??? register_archive_format(name, function, extra_args=None, description='')
??????? Registers an archive format.
?????? ?
??????? name is the name of the format. function is the callable that will be
??????? used to create archives. If provided, extra_args is a sequence of
??????? (name, value) tuples that will be passed as arguments to the callable.
??????? description can be provided to describe the format, and will be returned
??????? by the get_archive_formats() function.
rmtree(path, ignore_errors=False, οnerrοr=None)
??????? Recursively delete a directory tree.
?????? ?
??????? If ignore_errors is set, errors are ignored; otherwise, if onerror
??????? is set, it is called to handle the error with arguments (func,
??????? path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
??????? path is the argument to that function that caused it to fail; and
??????? exc_info is a tuple returned by sys.exc_info().? If ignore_errors
??????? is false and onerror is None, an exception is raised.
?? ?
??? unregister_archive_format(name)
DATA
??? __all__ = ['copyfileobj', 'copyfile', 'copymode', 'copystat', 'copy', ...
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的os模块中的shutil的使用方式与方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习笔记系列-方便自我学习
- 下一篇: 作者:钱卫宁(1976-),男,华东师范