Python is very similar to Perl so to save myself some time I would do copy/paste/replace… 🙂
Python is scripting language; that means you don’t compile it to executable, like C/C++, nor compile it to binary file which executed by special environment, like Java of .Net Core. Instead you write a script which you feed to Python interpreter, like Bash of batch scripts.
Installing Python very easy:
– for Unix use sudo apt-get install python or sudo yum install python
– for Windows installer is available here (don’t forget to enable setting up environment variable, or add it to the system PATH manually)
and on to our first Hello World:
print ("Hello World from Python!")
save it as hello.py and execute with
On Unix you can add the first line which would tell what application (Python in our case) to execute the script.
#!/usr/bin/env python print ("Hello World from Python!")
if you don’t have env program you can specify path directly:
#!/usr/bin/python print ("Hello World from Python!")
don’t forget to change file mode with chmod 755 hello.py and now you can just run
On Windows, file association with Python interpreter usually works pretty well.
Let’s see somewhat real life scenario. Recently I found out that Vocabulary Builder doesn’t work on my Kindle Oasis if book format is mobi, but works fine if it’s azw3 format. So let’s write a script which would convert all mobi books in the current directory to awz3 format. To convert we will use Calibre:
import os import re import subprocess converter = r'"C:\\Program Files (x86)\\Calibre2\\ebook-convert.exe"' for mobi in os.listdir("."): if mobi.endswith(".mobi"): azw3 = re.sub(r'.mobi$', '.azw3', mobi) print('converting ' + mobi + ' ' + azw3) subprocess.call(converter+ ' "' + mobi + '" "' + azw3 + '"')
If you want to learn more about Python here is great resource.