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

相关文章
|
3天前
|
数据挖掘 Python
🚀告别繁琐!Python I/O管理实战,文件读写效率飙升的秘密
在日常编程中,高效的文件I/O管理对提升程序性能至关重要。Python通过内置的`open`函数及丰富的库简化了文件读写操作。本文从基本的文件读写入手,介绍了使用`with`语句自动管理文件、批量读写以减少I/O次数、调整缓冲区大小、选择合适编码格式以及利用第三方库(如pandas和numpy)等技巧,帮助你显著提升文件处理效率,让编程工作更加高效便捷。
16 0
|
1天前
|
数据可视化 Python
Python编程中的数据可视化技术
【9月更文挑战第19天】在数据驱动的时代,将复杂的数据集转化为直观易懂的视觉表达至关重要。本文将深入探索Python中的数据可视化库,如Matplotlib和Seaborn,并指导读者如何运用这些工具来揭示数据背后的模式和趋势。文章不仅会介绍基础图表的绘制方法,还将讨论高级技巧以提升图表的信息丰富度和吸引力。
|
2天前
|
人工智能 数据挖掘 开发者
Python编程入门:从基础到实战
【9月更文挑战第18天】本文将带你走进Python的世界,从最基本的语法开始,逐步深入到实际的项目应用。无论你是编程新手,还是有一定基础的开发者,都能在这篇文章中找到你需要的内容。我们将通过详细的代码示例和清晰的解释,让你轻松掌握Python编程。
16 5
|
1天前
|
API 开发者 Python
Python高手修炼手册:精通文件系统操作,掌控I/O管理,提升编程效率
在Python编程中,从初学者成长为高手,关键在于深入理解底层细节并熟练运用高效工具。本文通过对比分析,探讨如何从基础出发,逐步精通文件系统操作与I/O管理,显著提升编程效率。文件系统操作方面,pathlib模块相较于传统的os和os.path模块更为直观易用;在I/O管理上,异步I/O相比同步I/O能大幅提升程序的并发能力和响应速度。通过这些技巧,开发者不仅能优化代码结构,还能预见并解决潜在性能问题,实现从细节到全局的全面提升。
9 3
|
2天前
|
Python
探索Python编程中的装饰器
【9月更文挑战第18天】本文将深入探讨Python中的一项强大功能——装饰器。通过简化的实例,我们会了解如何创建和使用装饰器来增强函数的功能,同时保持代码的整洁性和可读性。
11 3
|
3天前
|
机器学习/深度学习 数据采集 存储
Python编程入门:从基础到实战
【9月更文挑战第17天】本文将带你进入Python的世界,从最基础的语法开始,逐步深入到实战项目。我们将一起探索Python的强大功能和灵活性,以及如何利用它解决实际问题。无论你是编程新手,还是有一定经验的开发者,都能在这篇文章中找到有价值的内容。让我们一起开启Python的学习之旅吧!
|
3天前
|
存储 数据挖掘 测试技术
Python接口自动化中操作Excel文件的技术方法
通过上述方法和库,Python接口自动化中的Excel操作变得既简单又高效,有助于提升自动化测试的整体质量和效率。
12 0
|
4天前
|
存储 程序员 Python
Python编程入门:从零到英雄
【9月更文挑战第16天】本文是一篇针对初学者的Python编程入门指南,旨在帮助读者从零基础开始,通过简单易懂的语言和实例,逐步掌握Python编程的基本知识和技能。文章首先介绍了Python的起源和特点,然后详细讲解了Python的安装、基本语法、数据类型、控制结构、函数、模块等基础知识,最后通过一个简单的项目实例,展示了如何运用所学知识解决实际问题。全文通俗易懂,结构清晰,适合所有对Python感兴趣的读者阅读和学习。
|
Windows Python Shell