使用shutil.copy复制文件时获取管理员密码? [英] Getting admin password while copy file using shutil.copy?

查看:124
本文介绍了使用shutil.copy复制文件时获取管理员密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python的shutil.copy复制文件列表.但是,当我将文件复制到/usr/lib/位置时,由于要成为管理员才能被拒绝权限.

I m using shutil.copy from python to copy a list of files. But when i copy the files to /usr/lib/ location, i m getting permission denied as i need to be an administrator to do that.

那么我该如何复制具有管理员权限的文件或 我如何从用户那里获得管理员密码来复制文件?

So How could i copy files with admin permission or how could i get the admin password from the user to copy the files?

想法将不胜感激

推荐答案

要添加到 katrielalex 所说的内容中:如果需要,可以使脚本通过sudo自身运行.这是概念证明:

To add to what katrielalex said: you can make the script run itself via sudo if you want. Here's a proof of concept:

import sys, os, subprocess

def do_root_stuff():
    print('Trying to list /root:')
    for filename in os.listdir('/root'):
        print(filename)

if __name__ == '__main__':
    print('Running as UID %d:' % os.geteuid())

    if os.geteuid() == 0:
        do_root_stuff()
    else:
        subprocess.check_call(['sudo', sys.executable] + sys.argv)

这篇关于使用shutil.copy复制文件时获取管理员密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆