美文网首页python百例
42-shutil模块常用方法

42-shutil模块常用方法

作者: 凯茜的老爸 | 来源:发表于2018-07-31 08:47 被阅读7次
    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')
    shutil.copy('/etc/shadow', '/tmp/')  # cp /etc/shadow /tmp/
    shutil.copy2('/etc/shadow', '/tmp/')  # cp -p /etc/shadow /tmp/
    shutil.move('/tmp/mima.txt', '/var/tmp/')  # mv /tmp/mima.txt /var/tmp/
    shutil.copytree('/etc/security', '/tmp/anquan') # cp -r /etc/security /tmp/anquan
    shutil.rmtree('/tmp/anquan')  # rm -rf /tmp/anquan
    # 将mima2.txt的权限设置成与/etc/shadow一样
    shutil.copymode('/etc/shadow', '/tmp/mima2.txt')
    # 将mima2.txt的元数据设置成与/etc/shadow一样
    # 元数据使用stat /etc/shadow查看
    shutil.copystat('/etc/shadow', '/tmp/mima2.txt')
    shutil.chown('/tmp/mima2.txt', user='zhangsan', group='zhangsan')
    

    相关文章

      网友评论

        本文标题:42-shutil模块常用方法

        本文链接:https://www.haomeiwen.com/subject/ndlcvftx.html