日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

os模块中的shutil的使用方式与方法

發布時間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 os模块中的shutil的使用方式与方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#os 模塊os.sep 可以取代操作系統特定的路徑分隔符。windows下為 '\\' os.name 字符串指示你正在使用的平臺。比如對于Windows,它是'nt',而對于Linux/Unix用戶,它是 'posix' os.getcwd() 函數得到當前工作目錄,即當前Python腳本工作的目錄路徑 os.getenv() 獲取一個環境變量,如果沒有返回none os.putenv(key, value) 設置一個環境變量值 os.listdir(path) 返回指定目錄下的所有文件和目錄名 os.remove(path) 函數用來刪除一個文件 os.system(command) 函數用來運行shell命令 os.linesep 字符串給出當前平臺使用的行終止符。例如,Windows使用 '\r\n',Linux使用 '\n' 而Mac使用 '\r' os.path.split(path) 函數返回一個路徑的目錄名和文件名 os.path.isfile() 和os.path.isdir()函數分別檢驗給出的路徑是一個文件還是目錄 os.path.exists() 函數用來檢驗給出的路徑是否真地存在 os.curdir 返回當前目錄 ('.') os.mkdir(path) 創建一個目錄 os.makedirs(path) 遞歸的創建目錄 os.chdir(dirname) 改變工作目錄到dirname os.path.getsize(name) 獲得文件大小,如果name是目錄返回0L os.path.abspath(name) 獲得絕對路徑 os.path.normpath(path) 規范path字符串形式 os.path.splitext() 分離文件名與擴展名 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, 如果用戶有權限的話,它將被安靜的替換. 操作將會失敗在某些Unix 中如果src和dst在不同的文件系統中. 如果成功, 這命名操作將會是一個原子操作 (這是POSIX 需要). 在 Windows上, 如果dst已經存在, 將拋出OSError,即使它是一個文件. 在unix,Windows中有效。 os.renames(old, new) 遞歸重命名文件夾或者文件。像rename()# shutil 模塊 shutil.copyfile( src, dst) 從源src復制到dst中去。當然前提是目標地址是具備可寫權限。拋出的異常信息為IOException. 如果當前的dst已存在的話就會被覆蓋掉 shutil.move( src, dst) 移動文件或重命名 shutil.copymode( src, dst) 只是會復制其權限其他的東西是不會被復制的 shutil.copystat( src, dst) 復制權限、最后訪問時間、最后修改時間 shutil.copy( src, dst) 復制一個文件到一個文件或一個目錄 shutil.copy2( src, dst) 在copy上的基礎上再復制文件最后訪問時間與修改時間也復制過來了,類似于cp –p的東西 shutil.copy2( src, dst) 如果兩個位置的文件系統是一樣的話相當于是rename操作,只是改名;如果是不在相同的文件系統的話就是做move操作 shutil.copytree( olddir, newdir, True/Flase) 把olddir拷貝一份newdir,如果第3個參數是True,則復制目錄時將保持文件夾下的符號連接,如果第3個參數是False,則將在復制的目錄下生成物理副本來替代符號連接 shutil.rmtree( src ) 遞歸刪除一個目錄以及目錄內的所有內容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位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的os模块中的shutil的使用方式与方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。