Using sudo with Python script

前端 未结 11 2185
轻奢々
轻奢々 2020-11-22 15:10

I\'m trying to write a small script to mount a VirtualBox shared folder each time I execute the script. I want to do it with Python, because I\'m trying to learn it for scri

11条回答
  •  感情败类
    2020-11-22 16:01

    Please try module pexpect. Here is my code:

    import pexpect
    remove = pexpect.spawn('sudo dpkg --purge mytool.deb')
    remove.logfile = open('log/expect-uninstall-deb.log', 'w')
    remove.logfile.write('try to dpkg --purge mytool\n')
    if remove.expect(['(?i)password.*']) == 0:
        # print "successfull"
        remove.sendline('mypassword')
        time.sleep(2)
        remove.expect(pexpect.EOF,5)
    else:
        raise AssertionError("Fail to Uninstall deb package !")
    

提交回复
热议问题