As promised I'm going to let you guys in to some very useful modules built-in with Python.
Importing (loading) a module:
You should know this by now, but in case you don't:
Code: Select all
import name_of_the_module
Code: Select all
import this_module as this_var
Code: Select all
import whatever_module
help(whatever_module)
The os module provides a set of OS-based commands, mostly file operations (remove, copy, chown, chmod....). Some examples:
Change directory:
Code: Select all
os.chdir(path)
Code: Select all
os.chdir("/") # in Linux
os.chdir("c:\") # in Windows
Code: Select all
os.chmod(path, mode)
# path must be a string, mode must be an integer
Code: Select all
os.chown(path, uid, gid)
Code: Select all
os.system("uname -a")
os.system("ls -a")
os.system("sudo apt-get install ubuntu-desktop")
os.system("vlc")
Code: Select all
if os.system("apt-get -v") != 0:
print "You don't have apt-get installed"
elif os.system("apt-get -v") == 0:
print "You have apt-get installed"
The random module is used to do things that involve random choices or generators, it can be used to create random numbers/strings or randomly select from a list.
Select from a list:
Code: Select all
x = [1,2,3,4,5,6,7,8,9,0]
random.choice(x)
Code: Select all
int("".join(random.sample("1234567890",10)))
Generate a random string:
Code: Select all
"".join(random.sample("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890",10))
Code: Select all
random.randrange(10)
zipfile:
A module for loading, extracting and creating .zip files.
Creating a file:
Code: Select all
zipf = zipfile.ZipFile(path_to_file, "w")
Code: Select all
zipf = zipfile.ZipFile(path_to_file, "r")
Code: Select all
x = "name of the file to be stored"
zipf.writestr(x, open(x, "rb").read())
Code: Select all
y = "Path to extract everything"
zipf.extractall(y)
Example:
We're going to create a file named test.txt, write something to that file, store it in a zip file named test.zip and extract it in a folder test:
Code: Select all
import os, zipfile
x = open("test.txt","w")
x.write("test")
x.close()
zipf = zipfile.ZipFile("test.zip", "w")
zipf.writestr("test.txt", open("test.txt", "rb").read())
os.mkdir("test")
zipf.extractall("test")
Mostly a module for conecting to a URL, it can be used to download a file:
Code: Select all
url = "www.some.url.of.a.file.com"
dl = urllib2.urlopen(url)
dl_file = open("whatever_file","w")
dl_file.write(dl.read())
dl_file.close()
A very little used yet powerful module, it provides a wrapper for some os functions to work on files an folders, some functions:
shutil.copyfile(source, destiny) --> copy a file from source to destiny
shutil.copytree(source, destiny) --> recursively copy a folder from source to destiny
shutil.move(source, destiny) --> recursively move a directory/file from source to destiny
shutil.rmtree(directory) --> (recursively) remove a directory
sys:
This module includes functions that interact with the Python interpreter itself, normally you don't need to use it, except the function sys.exit() which will terminate your app:
Code: Select all
sys.exit("exiting...")
Tkinter is the default GUI toolkit with Python, it doesn't have much in terms of widgets, but it can be useful for quick and simple GUIs. I will explain this module more in-depth later.
That's all for now.
Previous: viewtopic.php?f=37&t=11904
Next: viewtopic.php?f=37&t=13056