-2

I just learned python. I want to make a python program to setup my ubuntu linux distribution.

How to simulate to enter "apt-get install firefox" and an enter key?

When installation is sucessful, "finished!" shows on the terminal. How my python program can know key word "finished" is displayed?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
ZIYIM
  • 11
  • Take a look at the `subprocess` module for executing shell commands from Python. – Barmar Jan 16 '17 at 22:37
  • Possible duplicate of [mod\_python equivalent to php exec() command](http://stackoverflow.com/questions/5185958/mod-python-equivalent-to-php-exec-command) – Redet Getachew Jan 16 '17 at 22:39
  • 1
    Check out `subprocess.Popen` examples, especially the ones that use `communicate`. It will run a program and give you its output. – tdelaney Jan 16 '17 at 23:01
  • You needn't 'shell out' to install a package using apt. There is an apt [package](https://launchpad.net/python-apt/) available. You can just install the package using [normal python code](http://stackoverflow.com/questions/17537390/how-to-install-a-package-using-the-python-apt-api). Disclaimer I haven't tried it. – Paul Rooney Jan 16 '17 at 23:32
  • `subprocess` is what I want. Thanks! – ZIYIM Jan 16 '17 at 23:50

1 Answers1

0

The terminal is a just way for a person to issue commands to the operating system and for the operating system to show results back to the person. With Python and other languages, you can issue commands to the operation system without a terminal and programmatically process responses received back from the OS or the program you had the OS run. For python, use the subprocess module. For example, you might write something like subprocess.POpen(["apt-get", "install", "firefox"], ...). I am on Windows, so cannot test a specific example on Ubuntu. Read the docs and experiment with the examples and options.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52