10 python tricks that will be useful for your scripts

Hello! It’s been a while since we’ve written about Python, so today we bring you these 10 Python tricks that will be useful for your scripts.

10 trucos de python que te serán utiles para tus scripts

Remember that if you like our articles you can comment on them and we thank you for sharing them on social networks to help us reach more people.

Let’s get started!

1. Read and write GZip files

Sometimes we need to create GZip files from a larger file or we may need to transfer data. This is the easiest way to create GZip files. It is like creating a text file but importing the gzip module:

f = gzip.open(indexpath, 'rb')
import gzip
contenido = "Texto"
f = gzip.open(indexpath, 'rb')
data = f.read()

To read it is exactly as easy, as well as writing it is the same way of working as when creating files with python:

import gzip
f = gzip.open(indexpath, 'rb')
data = f.read()

2. Create tar.gz file

As in the previous example we will create a GZip file but from a directory instead of from a file. In this example we will create a tar.gz file in the home directory of the current user including in the file the .ssh directory which is also in the home directory of the current user:

from shutil import make_archive
import os
archive_name = os.path.expanduser(os.path.join('~', 'miarchivo'))
root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
make_archive(archive_name, 'gztar', root_dir)

3. Hide the password when prompted by console

In this example we show how to hide the password when asking for it by console in one of our scripts. This is useful to prevent it from being displayed. It will behave exactly as the passwd or su or sudo commands do when asking for the password.

The code is as follows:

import getpass
usuario = raw_input("Introduce usuario: ")
password = getpass.getpass("Introduce password: ")
print(usuario, password)

The following would be displayed:


4. Executing commands with pexpect

This module is the Python version of expect. Basically it is able to Execute a command and according to the output of the command enter a text in the console. This is valid when using commands that require a timeout for entering parameters such as passwd or ssh. This way you can complete the execution of the command correctly by entering the necessary parameters, in the example we will show how to execute SSH by entering the password of the target host with pexpect:

import pexpect
import time
cmd = "ssh root@"
child = pexpect.spawn(cmd)
child.expect('password:', timeout=10)
child.sendline("ping -c1\n")
fout = file('/tmp/texto-','w')
child.logfile = fout

What we would see in the execution would be as follows:



And the log file would be:

pexpect log

5. Encode and decode a base64 binary by chunks

These codes allow to encode and decode files in base64, it is useful to transfer large files or to transfer binary files or to save virus signatures, etc:

The way to encode it would be as follows, in which we are encoding a binary of the ping command stored in /tmp:

import codecs
import binascii

byte = "1"
in_file = open("/tmp/ping","rb")
out_file = open("/tmp/ping.b64","wb")
while byte != "":
    byte = in_file.read(128)
    encode = codecs.encode(binascii.b2a_base64(byte),"base64")
    out_file.write("__INICIO__" + encode[0:(len(encode)-2)] )

As you can see in the above code, it opens the source file and the destination file. It reads a block from the source file, encodes each block and saves it in the destination file.

In the next code block, it does just the opposite. That is, it opens the encoded file, decodes it and writes it to the destination file. All this block by block.

import codecs
import binascii

byte = "1"
in_file = open("/tmp/ping.b64","rb")
out_file = open("/tmp/ping.devuelta","wb")
count = 0 
while byte != "":
    byte = in_file.read(244)
    print byte
    if len(byte) != 244:
        byte = byte[0:len(byte)-1]
    if byte[0:10] == "__INICIO__": 
        byte = byte.replace("__INICIO__","")
        print "Entra1"
        bytes = byte.split("__INICIO__")
        for b in bytes:
            print b

6. Comprobar web

This code block helps to see if a website has problems. In this case it is tested for only 10 minutes but by tweaking the code it can be tested infinitely:

import requests,time,sys

def getWeb(url):
    r = requests.get(url)
    return int(r.status_code)

def main():
    url = "http://google.es" #sys.argv[1]
    counter = 0
    status = getWeb(url)
    print ("The website returns status code: {0}".format(status))
    while counter<10 and status==500:
        status = getWeb(url)
        print ("The website returns status code: {0}".format(status))
        counter += 1
    if status==500:
        print ("The website is down")
    elif status==200:
        print ("The website is OK")
        print ("The website is not returning error code 500 but is not OK")

if __name__=="__main__":


7. Getting a voice from a text

Unfortunately I don’t have an alternative for linux yet, I only have this code available for windows.

This code plays the text that is passed as a parameter as a sound:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

while True:
    print("Introduzca el texto a reproducir:")
    s = raw_input()


8. Reimport module

Sometimes a module has to be reloaded at runtime, this is achieved with:


With this line we can make that if we have created a service, it reloads modules at runtime, to reload possible changes in modules or plugins without restarting the service.

9. Remove non-alphanumeric characters

This code replaces the non-alphanumeric characters by the character we want, this can be used for example as a filter for passwords:

import re
s = re.sub('[^0-9a-zA-Z]+', '*', s)

10. Remove empty items from a list

Sometimes when we replace elements of a list or we make a split of a text it can happen that we generate empty elements of the list in the form: [”]. This can be solved by eliminating these elements with the following code:

texto="Prueba con     espacios  multiples"
str_list=texto.split(" ")
#este es el mas rapido
str_list = filter(None, str_list) # fastest
#O podemos utilizar este:
str_list = filter(bool, str_list) # fastest
#Este es otra opcion:
str_list = filter(len, str_list) # a bit slower
#O este otro:
str_list = filter(lambda item: item, str_list) # slower than list 

Here we can see how it works:


That’s all for today. If you liked it or want more articles like this one leave your opinion in a comment and/or share in your social networks.
See you soon!

Leave a Reply