Learn Python Control Flow and Loops to Write and Tune Shell Scripts – Part 2

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

7 Responses

  1. steve says:


    Don’t get why this script isn’t working, even after correcting for indentation and adding utf-8 it only runs under IDE’s.

    At the console I still get…..

    ./uname.py: line 1: #!/usr/bin/python: No such file or directory

    I only code in 2.7 hence the change or omission of line one. I Chmod it and that is the correct path to python2.7.
    Yet this is the only python file run at the console that complains like this……….?

  2. Paul Campbell says:

    The web version does not include indentation where required by python syntax. I added the indentation and got past those errors and then got
    the following:

    SyntaxError: Non-ASCII character ‘\xc3’ in file uname.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    Also it will not run on windows because os.uname not available except on linux.

    I got around these errors by using pythonanywhere.com on the web. It ran and was neat but I would think some one at tecmint could help with
    the indentation problem and also specify it will not work on Windows.

    • Gabriel A. Cánepa says:

      Thanks for bringing this to our attention. I must confess I never thought I had to state explicitly this would work on Linux only, but we will add that notice as per your suggestion.
      Please add a small note to clarify. As for the indentation, perhaps we will need a different WP plugin. Any ideas?

  3. big joe says:

    Program will not run as shown due to no indentation starting with ‘if len(item) < len(headers[index]):'.

    • Gabriel A. Cánepa says:

      @big joe,
      It’s possible that the web formatting has damaged the indentation a little. But I can assure you that the program works, as you can see in the screenshots.

  4. Andy F says:

    I prefer to use subprocess.Popen to execute Linux commands:
    uptime = subprocess.Popen([‘uptime’, ‘-s’], stdout=subprocess.PIPE).communicate()[0]

    I would also recommend O’Reilly’s “Python for Unix and Linux System Administration”, by Noah Gift & Jeremy Jones. You can easily find .pdfs (if you don’t want to buy it).

    • @Andy,
      While that is true, on a personal note I prefer to keep things as simple as possible. If there is a Python-native way of executing a given command without spawning new process, I’d usually stick with it. Thank you for the book recommendation, I’ll take a look at it.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.