Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 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 at 20:43
    
Seconded, bash seems better. You could look at stackoverflow.com/questions/89228/… if you're bent on using python. –  muru Jul 8 at 20:43
    
My manager wants me to learn python instead of using bash, I'm a newbie intern. –  user114638 Jul 8 at 20:48
    
Google Python classes - seems as a good start. –  Hannu Jul 8 at 21:05

1 Answer 1

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 colors.red("""Your repository is dirty, commit any changes first""")
    sys.exit(1)

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.