东莞市做网站的公司哪家好,企业建站程序哪个好,东莞的公司,企业网络营销方案策划shutil模块
针对文件的拷贝#xff0c;删除#xff0c;移动#xff0c;压缩和解压操作 # 1.copyfileobj只能复制文件内容#xff0c;无法复制权限#复制文件时#xff0c;要选择自己有权限的目录执行操作#xff0c;创建的文件会根据系统umask设定的参数来指定用户权限
s…shutil模块
针对文件的拷贝删除移动压缩和解压操作 # 1.copyfileobj只能复制文件内容无法复制权限#复制文件时要选择自己有权限的目录执行操作创建的文件会根据系统umask设定的参数来指定用户权限
shell
[studentserver1 tmp]$ ll /etc/passwd
-rw-r--r-- 1 root root 2.7K 7月 15 09:02 /etc/passwd
#-----------------------------------------------------------
with open(/etc/passwd, moder) as fr:with open(/tmp/mypasswd,modew) as fw:shutil.copyfileobj(fr,fw)
#-----------------------------------------------------------
[studentserver1 tmp]$ ll /tmp/mypasswd
-rw-rw-r-- 1 student student 2.7K 11月 6 10:53 /tmp/mypasswd
# 2.copyfile只能复制文件内容无法复制权限
shell
[studentserver1 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)
#---------------------------------------------------------
[studentserver1 tmp]$ ll /etc/resolv.conf
-rw-r--r-- 1 root root 53 11月 6 07:52 /etc/resolv.conf
# 3.copy将文件src复制到文件或目录dst,包含权限
[studentserver1 tmp]$ ll /etc/hosts
-rw-r--r-- 1 root root 674 10月 31 09:50 /etc/hosts
#---------------------------------------------------------
shutil.copy(/etc/hosts,/tmp/myhosts)
#---------------------------------------------------------
[studentserver1 tmp]$ ll /tmp/myhosts
-rw-r--r-- 1 student student 674 11月 6 11:04 /tmp/myhosts
# 4.move递归将文件或目录移动到另一个位置并返回目标
[studentserver1 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)
#---------------------------------------------------------
[studentserver1 tmp]$ ll /tmp/myhosts
ls: 无法访问 /tmp/myhosts: 没有那个文件或目录
[studentserver1 tmp]$ ll /home/student/hosts.move
-rw-r--r-- 1 student student 674 11月 6 11:04 /home/student/hosts.move
# 5.copytree递归复制以src为根的整个目录树返回目标目录由dst命名的目标不能存在(存在会报错)复制时注意文件目录权限
shutil.copytree(/home/student,/tmp/security)
----------------------------------------------------------
# 6.rmtree删除整个目录树路径必须指向目录空目录或者非空目录都可使用
[studentserver1 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)
# ------------------------------------------------------------
[studentserver1 test]$ ll
总用量 0
# 7.copymode复制权限将权限位从src复制到dst,文件内容所有者和组不受影响
[studentserver1 test]$ ll
总用量 4.0K
-rw-r--r-- 1 student student 674 11月 6 11:35 hosts
[studentserver1 test]$ ll /etc/shadow
---------- 1 root root 1.5K 8月 12 06:52 /etc/shadow
#----------------------------------------------------
shutil.copymode(/etc/shadow,/tmp/test/hosts)
#----------------------------------------------------
[studentserver1 test]$ ll
总用量 4.0K
---------- 1 student student 674 11月 6 11:35 hosts
# 8.chown更改路径的所有者用户和组
[studentserver1 test]$ ll
总用量 4.0K
-rwxrwxrwx 1 student root 674 11月 6 11:35 hosts
# ---------------------------------------------------
shutil.chown(/tmp/test/hosts,userstudent,groupstudent)
# ------------------------------------------------------------
[studentserver1 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())
# remove删除文件
[studentserver1 first]$ pwd
/tmp/first
[studentserver1 first]$ ll
总用量 0
-rw-rw-r-- 1 student student 0 11月 6 14:48 a.txt
# ------------------------------------------------
os.remove(first/a.txt)
# ------------------------------------------------
[studentserver1 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))