import random
import string
# Generate a random password of length 10
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choices(characters, k=length))
password = generate_password(10)
print(password)Python tutorials with source code, examples, guides, and tips and tricks for Windows and Linux development.
Tuesday, October 6, 2026
Python 3.13.13 : Exemple with random and string.
Monday, October 7, 2019
Python 3.7.4 : Example with subprocess - part 001.
The source code is simple to understand.
The execute_proceess_with_communicate let run the ls command with the sudo user permissions:
import os
import sys
import string
import subprocess
import codecs
inp = ''
cmd = 'ls'
password = ''
def execute_proceess_with_communicate(inp):
"""Return a list of hops from traceroute command."""
p = subprocess.Popen(
['sudo', cmd, inp],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=False)
text, _ = p.communicate(password)
#print(type(text))
outp = codecs.decode(text,'utf8')
out_split=outp.split('\n')
return out_split
def normalize_out(list_outp):
"""Extract information from traceroute line per line."""
normalized_out = []
for op in list_outp:
# filer out if an empty line
if len(op) is 0:
continue
op_split = op.split()
normalized_out.append(op_split)
return normalized_out
if __name__ == '__main__':
inp = sys.argv[1]
out = execute_proceess_with_communicate(inp)
n_out = normalize_out(out)
print(n_out)
The result is this:[mythcat@desk scripts]$ python3 subprocess_001.py '/'
[['bin'], ['boot'], ['dev'], ['etc'], ['home'], ['lib'], ['lib64'], ['media'], ['mnt'], ['opt'], ['proc'],
['root'], ['run'], ['sbin'], ['srv'], ['sys'], ['tmp'], ['usr'], ['var']]
Thursday, June 13, 2013
What you need : stdout or print ?
My question is much more complicated than intended and I will show you in this tutorial.
Most users use print or print() - if it used in python 3.
For example you can use this without import any python module.
$ python
Python 2.6.8 (unknown, Apr 14 2013, 18:10:41)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "my text"
my text
>>> This is simple to show some strings.
What the most people don't know about print function it's the interface of stdout.write.
Let's see another example with stdout.write .
>>> sys.stdout.write(str(my text) + '\n')
File "", line 1
sys.stdout.write(str(my text) + '\n')
^
SyntaxError: invalid syntax
>>> sys.stdout.write(str("my text") + '\n')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'sys' is not defined First error tells us about function str.It's need just one arg not this my text.
The next error tells us about sys module is not import by python.
Note: This restriction can help us sometime.
Some example using stdout.write :
First is : my text example , see I add '\n' to go to the next row.
>>> import sys
>>> sys.stdout.write("my text"+'\n')
my text Let's try to change the output.
See the next image :

You can say : I can do this with print function.
>>> print "\033[0;32m"+"my text"+"\033[0m"
my text Yes! Right. Can you do this ? (see the next video)
Saturday, March 2, 2013
Simple way to reverse strings
Just see the next source code:
>>> text='0123456789'
>>> reverse_text=text[::-1]
>>> print reverse_text
9876543210
>>>