shutil模块
针对文件的拷贝,删除,移动,压缩和解压操作
```shell
[student@server1 tmp]$ ll /etc/passwd
-rw-r--r-- 1 root root 2.7K 7月 15 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 11月 6 10:53 /tmp/mypasswd
```shell
[student@server1 tmp]$ ll /tmp/myresolv.conf
-rw-rw-r-- 1 student student 53 11月 6 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 11月 6 07:52 /etc/resolv.conf
[student@server1 tmp]$ ll /etc/hosts
-rw-r--r-- 1 root root 674 10月 31 09:50 /etc/hosts
shutil.copy("/etc/hosts","/tmp/myhosts")
[student@server1 tmp]$ ll /tmp/myhosts
-rw-r--r-- 1 student student 674 11月 6 11:04 /tmp/myhosts
[student@server1 tmp]$ ll /tmp/myhosts
-rw-r--r-- 1 student student 674 11月 6 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 11月 6 11:04 /home/student/hosts.move
shutil.copytree("/home/student","/tmp/security")
----------------------------------------------------------
[student@server1 test]$ ll
总用量 8.0K
-rw-r--r-- 1 student student 674 11月 6 11:28 hosts
-rw-r--r-- 1 student student 53 11月 6 11:28 resolv.conf
shutil.rmtree("/tmp/test")
[student@server1 test]$ ll
总用量 0
[student@server1 test]$ ll
总用量 4.0K
-rw-r--r-- 1 student student 674 11月 6 11:35 hosts
[student@server1 test]$ ll /etc/shadow
---------- 1 root root 1.5K 8月 12 06:52 /etc/shadow
shutil.copymode("/etc/shadow","/tmp/test/hosts")
[student@server1 test]$ ll
总用量 4.0K
---------- 1 student student 674 11月 6 11:35 hosts
[student@server1 test]$ ll
总用量 4.0K
-rwxrwxrwx 1 student root 674 11月 6 11:35 hosts
shutil.chown("/tmp/test/hosts",user="student",group="student")
[student@server1 test]$ ll
总用量 4.0K
-rwxrwxrwx 1 student student 674 11月 6 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())
[student@server1 first]$ pwd
/tmp/first
[student@server1 first]$ ll
总用量 0
-rw-rw-r-- 1 student student 0 11月 6 14:48 a.txt
os.remove("first/a.txt")
[student@server1 first]$ ll
总用量 0
os.rmdir("first")
print("获取路径中携带的文件名:%s"%os.path.basename('/tmp/test/abc'))
print("获取路径中携带的文件目录:%s"%os.path.dirname("/tmp/test/abc"))
print(os.path.split("/tmp/test/abc"))
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'))