To all self-help authors who charge money for the simplest shit.[Not Serious]

This all seems like an even more creative way to waste time. Why should I sacrifice time,energy and money to buy your book and read it? How certain will I get what my money and time is worth? You see the  system is messed up because it allows con-artists such as yourself to come here and write all kinds of persuasive marketing fluff just to dupe innocent people out of their time and money into thinking that “this productive that costs a good amount of money is actually going to solve my problem”. This is the typical logic of self-help authors and the drooling junkies who fall on their faces after them. Every time I look on your website pages it is filled with lies. It’s  like you fuckers have no shame about how far you can stretch your unrealistic bullshit just to sell something. It’s ironic because just reading  and trying to figure out what is it you are actually trying to do would have been better spent being productive such as writing that python program or studying complex mathematical equations or some philosophical nonsense the cool kids like to call “Existentialism”. If I read another fucking website that comes up with lofty promises and is charging me money to get these lofty promises when there is no certainty or plausibility that what is being advertised actually works, I am going to go on a motherfucking killing spree. You know what you fuckers are doing? You are selling persuasion not actual value. You are not selling practical advice. Fuck off with the marketing. No you aren’t even doing marketing the correct fucking way. What you are giving isn’t value. I am not paying you money or spend my time on your books, your CDs and whatever goddamn bullshit you want to pull out of your snake oil salesman/new age/just-plain-crazy ass  just to fucking feel good. How are you fuckers any different from those nigerian scammers? You are only more persuasive and less obvious.

Python Cheatsheet

Python System Administration

The OS module in Python provides a way of using operating system dependent
functionality. 

The functions that the OS module provides allows you to interface with the
underlying operating system that Python is running on. (Windows, Mac or
Linux. 

You can find important information about your location or about the process. 

Before we start, make sure that you have imported the OS module "import os"

OS Functions explained

os.system()	# Executing a shell command

os.stat()	# Get the status of a file

os.environ()    # Get the users environment

os.chdir()   	# Move focus to a different directory

os.getcwd()    	# Returns the current working directory

os.getgid()    	# Return the real group id of the current process

os.getuid()    	# Return the current process’s user id

os.getpid()     # Returns the real process ID of the current process

os.getlogin()   # Return the name of the user logged

os.access()   	# Check read permissions

os.chmod()    	# Change the mode of path to the numeric mode

os.chown()   	# Change the owner and group id

os.umask(mask)  # Set the current numeric umask

os.getsize()   	# Get the size of a file

os.environ()    # Get the users environment

os.uname()   	# Return information about the current operating system

os.chroot(path) # Change the root directory of the current process to path

os.listdir(path)# List of the entries in the directory given by path

os.getloadavg() # Show queue averaged over the last 1, 5, and 15 minutes

os.path.exists()# Check if a path exists

os.walk()   	# Print out all directories, sub-directories and files

os.mkdir(path)	# Create a directory named path with numeric mode mode

os.remove(path)	# Remove (delete) the file path

os.rmdir(path)  # Remove (delete) the directory path

os.makedirs(path)# Recursive directory creation function

os.removedirs(path) # Remove directories recursively

os.rename(src, dst) # Rename the file or directory src to dst

OS Functions Examples

Let's get started to see how we can use these OS functions.
Get current working directory with os.getcwd()
1
print os.getcwd()
Get the status of a file with os.stat()
1
print "Getting the status of: ", os.stat('/usr/bin/python')
Execute a shell command with os.system()
1
os.system('ls -l')
Return the current process id with os.getpid()
1
print os.getpid()
1
os.chmod(path, mode)
Change the owner and group id of path to the numeric uid and gid with os.chown()
1
os.chown(path, uid, gid)
Processes in the system run queue averaged over the last 1, 5, and 15 minutes
1
print os.getloadavg()
Check if a path exists with os.path.exists()
1
if os.path.exists("file.txt"):
Create a new directory named 'new_directory' if it doesn't exist already"
1
os.path.exists("new_directory") or os.mkdir("new_directory")
Check if the path is a directory or a file with os.path.isdir() & os.path.isfile()
1
2
3
path = "/tmp"
if os.path.isdir(path): print "That's a directory"
if os.path.isfile(path): print "That's a file"
Create a directory with os.makedir()
1
print os.mkdir('new_directory', 0666)
Recursive create directories with os.makedirs()
1
os.makedirs('dir_a/dir_b/dir_c')
Remove a directory with os.rmdir()
1
print os.rmdir('directory')
Recursively remove empty directories with os.rmdirs()
1
os.removedirs('dir_a/dir_b/dir_c')
Rename a file with os.rename()
1
print os.rename('/path/to/old/file', '/path/to/new/file')
Rename a file with shutil.move()
1
print shutil.move('/path/to/old/file', '/path/to/new/file')
Rename a file with shutil.copy()
1
print shutil.copy('/path/to/old/file', '/path/to/new/file')
Get the users home directory
1
print os.path.expanduser('~')
Check read permissions with os.access()
1
2
path = '/tmp/file.txt'
print os.access(path, os.R_OK)
Get the users environment with os.environmen()
1
2
home =  os.environ['HOME']
print home
Move focus to a different directory with os.chdir()
1
print os.chdir('/tmp')
Print out all directories, sub-directories and files with os.walk()
1
2
3
4
for root, dirs, files in os.walk("/tmp"):
    print root
    print dirs
    print files
Get the last time a directory was accessed with os.path.getatime()
1
os.path.getatime('/tmp')
Get the last time a directory was modified with os.path.getmtime()
1
os.path.getmtime('/tmp')
Get the user ID with os.getuid()
1
if os.getuid() != 0: print "you are not root"
Get the group ID with os.getgid()
1
print os.getgid()
Return the name of the user logged in with os.getlogin()
1
print os.getlogin()
Returns a list of all files in a directory with os.listdir()
1
2
for filename in os.listdir("/tmp"):
    print "This is inside /tmp", filename
Get the size of a file with os.path.getsize()
1
path.getsize("/tmp/file.txt")
Using Python in your daily work is a good way to automate system administration
tasks, when you feel that your shell scripts are to limited.

Python Programming Dictionaries

Dictionaries have keys and values. The keys are used to find the values. Here is an
example of a dictionary in use:

def print_menu():
print ’1. Print Phone Numbers’
print ’2. Add a Phone Number’
print ’3. Remove a Phone Number’
print ’4. Lookup a Phone Number’
print ’5. Quit’
print
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
menu_choice = input(“Type in a number (1-5):”)
if menu_choice == 1:
print “Telephone Numbers:”
for x in numbers.keys():
print “Name: “,x,” \tNumber: “,numbers[x]
print
elif menu_choice == 2:
print “Add Name and Number”
name = raw_input(“Name:”)
phone = raw_input(“Number:”)
numbers[name] = phone
elif menu_choice == 3:
print “Remove Name and Number”
name = raw_input(“Name:”)
if numbers.has_key(name):
del numbers[name]
else:
print name,” was not found”
elif menu_choice == 4:
print “Lookup Number”
name = raw_input(“Name:”)
if numbers.has_key(name):
print “The number is”,numbers[name]
else:
print name,” was not found”
elif menu_choice != 5:
print_menu()

And here is my output:

1.
2.
3.
4.
5.
Print Phone Numbers
Add a Phone Number
Remove a Phone Number
Lookup a Phone Number
Quit
Type in a number (1-5):2
Add Name and Number
Name:Joe
Number:545-4464
Type in a number (1-5):2
Add Name and Number
Name:Jill
Number:979-4654
Type in a number (1-5):2
Add Name and Number
Name:Fred
Number:132-9874
Type in a number (1-5):1
Telephone Numbers:
Name: Jill
Number: 979-4654
Name: Joe
Number: 545-4464
Name: Fred
Number: 132-9874
Type in a number (1-5):4
Lookup Number
Name:Joe
The number is 545-4464
Type in a number (1-5):3
Remove Name and Number
Name:Fred
Type in a number (1-5):1
Telephone Numbers:
Name: Jill
Number: 979-4654
Name: Joe
Number: 545-4464
Type in a number (1-5):5

 

Python Tutorial for Beginners

Introduction to Programming using Python

Sqllite

Applied Python