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

本文详细介绍了使用Python进行文件及目录的操作方法,包括文件复制、移动、删除、压缩与解压等功能。通过示例展示了如何利用shutil模块完成这些任务,并提供了zipfile和tarfile模块的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作目录及文件

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.
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值