本文主要是介绍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模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!