python的shutil模块

2023-12-11 23:31
文章标签 python 模块 shutil

本文主要是介绍python的shutil模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先import导入该模块

import shutil

一. copyfileobj

只复制文件内容,不负责权限

[root@rhel8 day05]# ls /opt
dc.txt  myweb.log  n1.log  n2.log  tc.txt
[root@rhel8 day06]# vim demo01.py 
import shutil
f1 = open("/etc/hosts",mode="r")
f2 = open("/opt/myhosts",mode="w")
shutil.copyfileobj(f1,f2)
f1.close()
f2.close()[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /etc/hosts
-rw-r--r--. 1 root root 158 9月  10 2018 /etc/hosts
[root@rhel8 day06]# ls -l /opt |grep hosts
-rw-r--r-- 1 root root 158 12月 11 21:02 myhosts

二.copyfile

只需要输入源文件地址与目标

[root@rhel8 day06]# ll /opt/myls
ls: 无法访问'/opt/myls': 没有那个文件或目录
[root@rhel8 day06]# vim demo01.pyimport shutil
shutil.copyfile("/usr/bin/ls","/opt/myls")[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /opt/myls
-rw-r--r-- 1 root root 166448 12月 11 21:08 /opt/myls
[root@rhel8 day06]# ll /usr/bin/ls
-rwxr-xr-x. 1 root root 166448 1月  11 2019 /usr/bin/ls

 三.copy

输入源文件地址与目标文件地址,复制内容和权限

[root@rhel8 day06]# ll /usr/bin/ls
-rwxr-xr-x. 1 root root 166448 1月  11 2019 /usr/bin/ls
[root@rhel8 day06]# ll /opt/myls2
ls: 无法访问'/opt/myls2': 没有那个文件或目录
[root@rhel8 day06]# vim demo01.py 
import shutil
shutil.copy("/usr/bin/ls","/opt/myls2")[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /opt/myls2
-rwxr-xr-x 1 root root 166448 12月 11 21:11 /opt/myls2

四.move

文件的移动

[root@rhel8 day06]# ll /mnt/myls2
ls: 无法访问'/mnt/myls2': 没有那个文件或目录
[root@rhel8 day06]# vim demo01.py 
import shutil
shutil.move("/opt/myls2","/mnt/myls2")
[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /mnt/myls2
-rwxr-xr-x 1 root root 166448 12月 11 21:11 /mnt/myls2

 五.copytree

目录的复制

[root@rhel8 day06]# ls -l /opt/security
ls: 无法访问'/opt/security': 没有那个文件
[root@rhel8 day06]#  vim python3 demo01.py
import shutil
shutil.copytree("/etc/security","/opt/security")
[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ls  /opt/security
access.conf       console.perms    limits.conf     namespace.init  pwquality.conf.d
chroot.conf       console.perms.d  limits.d        opasswd         sepermit.conf
console.apps      faillock.conf    namespace.conf  pam_env.conf    time.conf
console.handlers  group.conf       namespace.d     pwquality.conf
#再次运行,查看结果[root@rhel8 day06]# python3 demo01.py 
Traceback (most recent call last):File "demo01.py", line 2, in <module>shutil.copytree("/etc/security","/opt/security")File "/usr/lib64/python3.6/shutil.py", line 321, in copytreeos.makedirs(dst)File "/usr/lib64/python3.6/os.py", line 220, in makedirsmkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/opt/security'
文件报错,显示文件已存在,要保证目标文件不存在

 六.rmtree

删除目录

[root@rhel8 day06]# ls /opt/security/
access.conf       console.perms    limits.conf     namespace.init  pwquality.conf.d
chroot.conf       console.perms.d  limits.d        opasswd         sepermit.conf
console.apps      faillock.conf    namespace.conf  pam_env.conf    time.conf
console.handlers  group.conf       namespace.d     pwquality.conf
[root@rhel8 day06]# vim demo01.py 
import shutil
shutil.rmtree("/opt/security")
[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ls /opt/security/
ls: 无法访问'/opt/security/': 没有那个文件或目录

关于权限

 一.copymode

只复制权限

#先查看/opt/myhosts的权限,是644
[root@rhel8 day06]# ll /opt/myhosts 
-rw-r--r-- 1 root root 158 12月 11 21:02 /opt/myhosts
#再查看/usr/bin/ls的权限,是755
[root@rhel8 day06]# ll /usr/bin/ls
-rwxr-xr-x. 1 root root 166448 1月  11 2019 /usr/bin/ls
[root@rhel8 day06]# vim demo01.py 
import shutil
shutil.copymode("/usr/bin/ls","/opt/myhosts") #把644权限变成给755
[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /opt/myhosts 
-rwxr-xr-x 1 root root 158 12月 11 21:02 /opt/myhosts

 二.chown

修改属主和属组

[root@rhel8 day06]# useradd dc #增加dc用户 
[root@rhel8 day06]# vim demo01.py 
import shutil
shutil.chown("/opt/myhosts",user='dc',group='dc')
[root@rhel8 day06]# python3 demo01.py 
[root@rhel8 day06]# ll /opt/myhosts
-rwxr-xr-x 1 dc dc 158 12月 11 21:02 /opt/myhosts

这篇关于python的shutil模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/482464

相关文章

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re