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.
"""

相关文章
|
2月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
385 7
|
2月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
403 4
|
2月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
312 0
|
2月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
219 0
|
Windows Python Shell
|
Python
Python 目录操作(转)
在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前目录('.
902 0
|
Python
python的目录操作
[1.os]1.重命名:os.rename(old, new)2.删除:os.remove(file)3.列出目录下的文件 :os.listdir(path)4.获取当前工作目录:os.getcwd()5.改变工作目录:os.chdir(newdir)6.创建多级目录:os.makedirs(r"c:/python /test")7.创建单个目录:os.mkdir("test")8.删除多个目录:os.removedirs(r"c:/python") #删除所给路径最后一个目录下所有空目录。
997 0
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
319 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
345 104

推荐镜像

更多