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版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数