Python学习-shutil模块和OS模块学习

shutil模块

针对文件的拷贝,删除,移动,压缩和解压操作

在这里插入图片描述

# 1.copyfileobj只能复制文件内容,无法复制权限

#复制文件时,要选择自己有权限的目录执行操作,创建的文件会根据系统umask设定的参数来指定用户权限
```shell
[student@server1 tmp]$ ll /etc/passwd
-rw-r--r-- 1 root root 2.7K  715 09:02 /etc/passwd
#-----------------------------------------------------------
with open("/etc/passwd", mode="r") as fr:
    with open("/tmp/mypasswd",mode="w") as fw:
        shutil.copyfileobj(fr,fw)
#-----------------------------------------------------------
[student@server1 tmp]$ ll /tmp/mypasswd 
-rw-rw-r-- 1 student student 2.7K 116 10:53 /tmp/mypasswd
# 2.copyfile只能复制文件内容,无法复制权限
```shell
[student@server1 tmp]$ ll /tmp/myresolv.conf 
-rw-rw-r-- 1 student student 53 116 11:01 /tmp/myresolv.conf
#---------------------------------------------------------
shutil.copyfile("/etc/resolv.conf","/tmp/myresolv.conf")
#---------------------------------------------------------
[student@server1 tmp]$ ll /etc/resolv.conf 
-rw-r--r-- 1 root root 53 116 07:52 /etc/resolv.conf
# 3.copy将文件src复制到文件或目录dst,包含权限
[student@server1 tmp]$ ll /etc/hosts 
-rw-r--r-- 1 root root 674 1031 09:50 /etc/hosts
#---------------------------------------------------------
shutil.copy("/etc/hosts","/tmp/myhosts")
#---------------------------------------------------------
[student@server1 tmp]$ ll /tmp/myhosts 
-rw-r--r-- 1 student student 674 116 11:04 /tmp/myhosts
# 4.move递归将文件或目录移动到另一个位置,并返回目标
[student@server1 tmp]$ ll /tmp/myhosts 
-rw-r--r-- 1 student student 674 116 11:04 /tmp/myhosts
#---------------------------------------------------------
shutil.move("/tmp/myhosts","/home/student/hosts.move")
#---------------------------------------------------------
[student@server1 tmp]$ ll /tmp/myhosts
ls: 无法访问 '/tmp/myhosts': 没有那个文件或目录
[student@server1 tmp]$ ll /home/student/hosts.move 
-rw-r--r-- 1 student student 674 116 11:04 /home/student/hosts.move
# 5.copytree递归复制以src为根的整个目录树,返回目标目录,由dst命名的目标不能存在(存在会报错),复制时注意文件目录权限
shutil.copytree("/home/student","/tmp/security")
----------------------------------------------------------
# 6.rmtree删除整个目录树,路径必须指向目录,空目录或者非空目录都可使用
[student@server1 test]$ ll
总用量 8.0K
-rw-r--r-- 1 student student 674 116 11:28 hosts
-rw-r--r-- 1 student student  53 116 11:28 resolv.conf
# -----------------------------------------------------------
shutil.rmtree("/tmp/test")
# ------------------------------------------------------------
[student@server1 test]$ ll
总用量 0
# 7.copymode复制权限,将权限位从src复制到dst,文件内容,所有者和组不受影响
[student@server1 test]$ ll
总用量 4.0K
-rw-r--r-- 1 student student 674 116 11:35 hosts
[student@server1 test]$ ll /etc/shadow
---------- 1 root root 1.5K  812 06:52 /etc/shadow
#----------------------------------------------------
shutil.copymode("/etc/shadow","/tmp/test/hosts")
#----------------------------------------------------
[student@server1 test]$ ll
总用量 4.0K
---------- 1 student student 674 116 11:35 hosts
# 8.chown更改路径的所有者用户和组
[student@server1 test]$ ll
总用量 4.0K
-rwxrwxrwx 1 student root 674 116 11:35 hosts
# ---------------------------------------------------
shutil.chown("/tmp/test/hosts",user="student",group="student")
# ------------------------------------------------------------
[student@server1 test]$ ll
总用量 4.0K
-rwxrwxrwx 1 student student 674 116 11:35 hosts

os 模块

在这里插入图片描述

print("当前工作目录:%s"%os.getcwd())
print("进程ID:%d" %os.getpid())
print("父进程ID:%d" %os.getppid())
print("系统环境变量:%s"%os.environ)
print("获取uname信息:%s"%str(os.uname()))
print("CPU核数:%s"%os.cpu_count())
print("查看当前目录下的文件:%s"%os.listdir("."))
os.chdir('/tmp/')
os.makedirs("./first")
print("当前目录:%s"%os.getcwd())
# remove删除文件
[student@server1 first]$ pwd
/tmp/first
[student@server1 first]$ ll
总用量 0
-rw-rw-r-- 1 student student 0 116 14:48 a.txt
# ------------------------------------------------
os.remove("first/a.txt")
# ------------------------------------------------
[student@server1 first]$ ll
总用量 0
# rmdir只能删除空目录
os.rmdir("first")
print("获取路径中携带的文件名:%s"%os.path.basename('/tmp/test/abc'))
print("获取路径中携带的文件目录:%s"%os.path.dirname("/tmp/test/abc"))
#split(),路径切割,从最右边/开始
print(os.path.split("/tmp/test/abc"))
#join()路径拼接
print(os.path.join("/tmp/test",'abc'))
# 判断路径是否为绝对路径
print(os.path.isabs('/tmp/test/abc'))
# 判断路径是否为相对路径
print(os.path.isdir('tmp/test/abc'))
# 判断是否为文件
print(os.path.isfile('tmp/test/abc'))
# 判断是否为链接文件
print(os.path.islink('tmp/test/abc'))
# 判断文件是否存在
print(os.path.exists('tmp/test/abc'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值