>>> import shutil >>> with open('/etc/passwd','rb') as sfobj: ... with open('/tmp/mima.txt','wb') as dfobj: ... shutil.copyfileobj(sfobj,dfobj) #拷贝文件对象 >>> shutil.copyfile('/etc/passwd','/tmp/mima2.txt') #拷贝,改名 '/tmp/mima2.txt' >>> shutil.copy('/etc/passwd','/tmp/') #拷贝到文件夹下,不改名 '/tmp/passwd' >>> shutil.copy2('/etc/passwd','/tmp/') #保持属性的拷贝,相当于cp -p '/tmp/passwd' >>> shutil.move('/tmp/mima.txt','/var/tmp/') #移动,相当于mv '/var/tmp/mima.txt' >>> shutil.copytree('/etc/security','/tmp/anquan') #拷贝目录,相当于cp -r '/tmp/anquan' >>> shutil.rmtree('/tmp/anquan') #删除目录,相当于rm -rf >>> shutil.copymode('/etc/shadow','/tmp/mima2.txt') #保持权限不变的拷贝 >>> shutil.copystat('/etc/shadow','/tmp/mima2.txt') #元数据要使用stat查看 >>> shutil.chown('/tmp/mima2.txt',user='zhangsan',group='zhangsan') #设置所属者,所属组
python-shutil模块常用方法(42)
最新推荐文章于 2024-08-22 19:23:28 发布