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

举报
彭世瑜 发表于 2021/08/13 23:11:06 2021/08/13
【摘要】 操作目录及文件 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.tx...

操作目录及文件

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")  # 解压文件

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

压缩文件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()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

压缩文件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()


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/79008245

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。