Python编程:shutil模块-操作目录及文件

简介: Python编程:shutil模块-操作目录及文件

操作目录及文件

import shutil
f1 = open("file.txt", "r", encoding="utf-8")
f2 = open("file_new.txt", "w", encoding="utf-8")
shutil.copyfileobj(f1, f2)  # 通过文件对象拷贝文件内容
shutil.copyfile("file.txt", "file_new.txt")  # 拷贝文件内容
shutil.copymode("file.txt", "file_new.txt")  # 仅拷贝权限
shutil.copystat("file.txt", "file_new.txt")  # 拷贝信息
shutil.copy("file.txt", "file_new.txt")  # 拷贝文件,包括权限
shutil.copy2("file.txt", "file_new.txt")  # 拷贝文件,包括全部信息
shutil.copytree("dir", "dir2")  # 拷贝目录及文件, 新文件不能存在
shutil.move("dir","dir2")  # 移动目录及文件
shutil.rmtree("dir2")  # 删除目录及文件
shutil.make_archive("dir1", "zip", "dir")  # 压缩文件
# (压缩后的文件名,文件格式,要压缩的文件路径)
shutil.unpack_archive("day5.zip", "dir", "zip")  # 解压文件

压缩文件zipfile

import zipfile
# 压缩
z = zipfile.ZipFile("dir5.zip", "w")
z.write("file.txt")
z.close()
# 解压
z = zipfile.ZipFile("dir5.zip", "r")
z.extractall()
z.close()

压缩文件tarfile

import tarfile
# 压缩
t = tarfile.open("dir1.tar", "w")
t.add("file.txt")
t.add("file_new.txt")
t.close()
# 解压
t = tarfile.open("dir1.tar", "r")
t.extractall()
t.close()

help(shutil)

"""
FUNCTIONS
    chown(path, user=None, group=None)
        Change owner user and group of the given path.
    copy(src, dst, *, follow_symlinks=True)
        Copy data and mode bits ("cp src dst"). Return the file's destination.  
    copy2(src, dst, *, follow_symlinks=True)
        Copy data and all stat info ("cp -p src dst"). Return the file's
        destination." 
    copyfile(src, dst, *, follow_symlinks=True)
        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, *, follow_symlinks=True)
        Copy mode bits from src to dst.
    copystat(src, dst, *, follow_symlinks=True)
        Copy all stat info (mode bits, atime, mtime, flags) from src to dst.        
    copytree(src, dst, symlinks=False, ignore=None, copy_function=<function copy2 at 0x0000000002A64A60>, ignore_dangling_symlinks=False)
        Recursively copy a directory tree.
    disk_usage(path)
        Return disk usage statistics about the given path.
    get_archive_formats()
        Returns a list of supported formats for archiving and unarchiving.
    get_unpack_formats()
        Returns a list of supported formats for unpacking.
    ignore_patterns(*patterns)
        Function that can be used as copytree() ignore parameter.
    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). 
    move(src, dst)
        Recursively move a file or directory to another location. This is
        similar to the Unix "mv" command. Return the file or directory's
        destination.
    register_archive_format(name, function, extra_args=None, description='')
        Registers an archive format.
    register_unpack_format(name, extensions, function, extra_args=None, description='')
        Registers an unpack format.
    rmtree(path, ignore_errors=False, None)
        Recursively delete a directory tree.
    unpack_archive(filename, extract_dir=None, format=None)
        Unpack an archive.
    unregister_archive_format(name)
    unregister_unpack_format(name)
        Removes the pack format from the registery.
    which(cmd, mode=1, path=None)
        Given a command, mode, and a PATH string, return the path which
        conforms to the given mode on the PATH, or None if there is no such
        file.
"""


相关文章
|
XML Shell API
python ConfigParser、shutil、subprocess、ElementTree模块简解
python ConfigParser、shutil、subprocess、ElementTree模块简解
|
安全 项目管理 Python
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
172 1
使用Python shutil库进行文件和目录操作
|
Python
Python之shutil库详解
Python之shutil库详解
327 3
|
Unix 数据安全/隐私保护 Python
30天拿下Python之shutil模块
30天拿下Python之shutil模块
168 0
|
Linux Shell Python
Python标准库分享之文件管理 (部分os包,shutil包)
Python标准库分享之文件管理 (部分os包,shutil包)
130 0
|
Go C# Python
Python 的 shutil 模块
`shutil`是Python标准库中的模块,提供高级文件和目录操作,如复制、移动、重命名、删除以及创建和删除目录。它扩展了`os`模块的功能,支持递归操作,例如`shutil.copytree()`用于递归复制目录,`shutil.rmtree()`用于递归删除目录。`shutil.move()`不仅移动文件,还可用于重命名。`shutil.remove()`和`shutil.rmtree()`分别用于删除文件和目录。这个模块对于文件管理任务非常实用。
260 5
|
Python
Python基础之shutil及zipfile模块
Python基础之shutil及zipfile模块
178 0
|
Python
Python中如何使用os模块和shutil模块处理文件和文件夹
os和shutil都是Python标准库中用于处理文件和文件夹的模块,它们都提供了许多常用的文件和文件夹操作功能,但是它们的使用场景和优势有所不同。os模块和shutil模块各自具有不同的优势,可以根据实际需要选择使用。如果只需要对单个文件或目录进行基本的文件操作,可以使用os模块;如果需要复制或移动多个文件或目录,或者需要进行文件和目录的压缩和解压缩,就应该使用shutil模块。有些需求同时使用两者才能满足要求
207 0
|
Python
shutil库:Python高级文件操作(二)
shutil库:Python高级文件操作(二)
193 0
shutil库:Python高级文件操作(二)
|
Unix Python
shutil库:Python高级文件操作(一)
shutil库:Python高级文件操作(一)
288 0
shutil库:Python高级文件操作(一)

推荐镜像

更多