Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to do a bunch of file copying, moving, svn check out commands, directory checking etc. and I'm trying to make a script to do it for me. I have basic python syntax, but I'm not sure how to use python to execute linux commands. I know the linux commands needed for example sudo apt-get install subversion i just don't know how to wrap python around it.

Specifically, could anyone give examples on how to:

  • Check if a package (like svn) is installed, if not then install it. I know this is a simple if-statement but no idea how to write it...
  • Check if a folder with a specific name is installed, if not create it on the desktop/some other directory. Similar to the above I would assume.
  • Move to a certain directory and execute a command (like I want to extract a .tgz file in a certain directory)

There's more but I want to grasp basic principles and actually learn how to execute linux commands using python scripting. Thanks for your time~

EDIT: I am specifically required to use python

share|improve this question

closed as off-topic by bain, qbi, Wilf, Eric Carvalho, Radu Rădeanu Jul 14 '14 at 12:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – bain, qbi, Wilf, Eric Carvalho, Radu Rădeanu
If this question can be reworded to fit the rules in the help center, please edit the question.

Is there a specific reason for doing this in Python? I'm asking as it sounds like bash is more suited to those three specific tasks. – Mendhak Jul 8 '14 at 20:43
Seconded, bash seems better. You could look at… if you're bent on using python. – muru Jul 8 '14 at 20:43
My manager wants me to learn python instead of using bash, I'm a newbie intern. – user114638 Jul 8 '14 at 20:48
Google Python classes - seems as a good start. – Hannu Jul 8 '14 at 21:05

You surely can do everything using Python's built-in stuff from os and subprocess modules.

A slightly tricky thing would be if you want your script to "interact" with the programs you're invoking (like "wait until it prints 'Press Enter to continue', then send it an Enter keypress) - for that you can use pexpect module.

For slightly higher-level way have a look at the Fabric library - besides running local commands it would also allow you to easily execute code on remote machines via SSH.

A small sample of code using Fabric:

if "dirty" in api.local("git describe --dirty", capture=True):
    print"""Your repository is dirty, commit any changes first""")

Reads almost like poetry:) Similar code in Bash would look like unintelligible mess in comparison...

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.