Reegan Miranda
Reegan Miranda

Reputation: 2949

How to give input streams in python?

i'm uninstall some packages in Linux via python os module my code like

def uninstallZdev():
    print 'Uninstallation as a Super User'
    system('apt-get remove xxx')

uninstallPackage()

but remove package ask like

After this operation, 2,621 kB disk space will be freed. Do you want to continue [Y/n]? how to give Y in program via python

Upvotes: 2

Views: 183

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1122392

Configure apt-get not to ask (see the apt-get man page:

apt-get --assume-yes remove xxx

For tools that cannot be configured, use pexpect to steer the process. pexpect lets you listen for output from a subprocess, and send input based on a simple API:

import pexpect

ag = pexpect.spawn('apt-get remove xxx')
ag.expect('Do you want to continue')
ag.send('Y')
ag.wait()
ag.close()

Upvotes: 4

Thorsten Kranz
Thorsten Kranz

Reputation: 12755

Use the -y to automate apt-get. Thus, you don't have to simulate the "y"-press.

Btw, I'd recommend using the subprocess-module, especially if you're planing to work with stdin/stdout.

And: be carefull! You seem to run this as root, or with sudo. If xxx happens to be libc-bin for some reason, you'll have fun restoring your system.

Upvotes: 0

Related Questions