Dispitus.com

Empowering change empowering Tech

Despite what assembly code and C coders may tell us, high-level languages ​​have their place in every programmer’s toolbox, and some of them are much more than a computing curiosity. Of the many high-level languages ​​we can choose from today, Python seems to be the most interesting for those who want to learn something new and do real work at the same time. Its no-nonsense implementation of object-oriented programming and clean, easy-to-understand syntax make it a language that is fun to learn and use, which is not something we can say about most other languages.

In Python Training, you will learn how to write applications that use command line options, read and write to pipes, access environment variables, handle interrupts, read and write to files, create temporary files, and write to system logs. In other words, you’ll find recipes for writing real apps instead of boring old Hello, World! stuff.

Starting

To start with, if you haven’t installed the Python interpreter on your system, now is the time. To make that step easier, install the latest Python distribution using packages compatible with your Linux distribution. rpm, deb, and tgz are also available on your Linux CD-ROM or online. If you follow the standard installation procedures, you shouldn’t have any problems.

I also recommend that you have the Python library reference handy; you may want it when the explanations given here do not meet your needs. You can find it in the same places as the Python Tutorial.

You can create scripts with your favorite text editor as long as you save the text in plain ASCII format and don’t automatically insert line breaks when the line is longer than the width of the editor window.

Always start your scripts with

#! /usr/local/bin/python
Prayed

#! /usr/bin/python

If the path to the python binary on your system is different, change that line, leaving the first two characters (#!) intact. Make sure this line is actually the first line of your script, not just the first non-blank line; It will save you a lot of frustration.
Use chmod to set the file permissions on your script to make it executable. If the script is just for you, type chmod 0700 scriptfilename.py; if you want to share it with other people in your group but not let them edit it, use 0750 as the chmod value; if you want to give everyone else access, use the value 0755. For help with the chmod command, type man chmod.

Reading options and arguments from the command line

Command line options and arguments are useful when we want to tell our scripts how to behave or pass some arguments to them (filenames, directory names, usernames, etc.). All programs can read these options and arguments if they wish, and your Python scripts are no different.

Implementing proper handlers boils down to reading the argv list and checking the options and arguments you want your script to recognize. There are a few ways to do this. Listing 1 is a simple option handler that recognizes the common -h, -help, and –help options, and when found, exits immediately after displaying the help message.[source]- https://www.linuxjournal.com/article/3946

Leave a Reply

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